Mur*_*gal 8 objective-c uigesturerecognizer ios6
allowableMovement我的财产UILongPressGestureRecognizer似乎被忽略了.我使用Single View Application模板创建了一个新项目(Xcode 4.5.1,iOS 6),并在视图中添加了Long Press Gesture Recognizer.有一个插座和一个动作.这是action方法的代码:
- (IBAction)longPress:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStatePossible) NSLog(@"possible");
if (sender.state == UIGestureRecognizerStateBegan) NSLog(@"began");
if (sender.state == UIGestureRecognizerStateChanged) NSLog(@"changed");
if (sender.state == UIGestureRecognizerStateRecognized) NSLog(@"recognized");
if (sender.state == UIGestureRecognizerStateCancelled) NSLog(@"cancelled");
if (sender.state == UIGestureRecognizerStateFailed) NSLog(@"failed");
CGPoint locationInView = [sender locationInView:self.view];
NSLog(@"long press: allowableMovement= %f, x= %f, y= %f", sender.allowableMovement, locationInView.x, locationInView.y);
}
Run Code Online (Sandbox Code Playgroud)
如果我按下足够长的时间然后松开,我会在日志中得到这个:
2012-10-30 20:24:41.449 Long Press[1078:907] began
2012-10-30 20:24:41.455 Long Press[1078:907] long press: allowableMovement= 10.000000, x= 210.500000, y= 99.500000
2012-10-30 20:24:42.880 Long Press[1078:907] recognized
2012-10-30 20:24:42.882 Long Press[1078:907] long press: allowableMovement= 10.000000, x= 208.500000, y= 96.000000
Run Code Online (Sandbox Code Playgroud)
这就是我所期待的.
但无论我设置为什么allowableMovement(正面,负面,大,小),一旦状态UIGestureRecognizerStateBegan,我可以在屏幕上拖动我的手指.状态更改为UIGestureRecognizerStateChanged并且频繁更新并且locationInView继续准确跟踪.当我放手时,我得到UIGestureRecognizerStateRecognized状态和最终输出到日志.
类引用表示如果移动超过,识别器应该失败allowableMovement.为什么allowableMovement房产似乎被忽略了?
Mur*_*gal 27
该allowableMovement属性与手势开始后可以拖动的距离无关.它是关于手势开始前你可以移动多远.
从课程参考:
当指定时间段(minimumPressDuration)按下允许的手指数(numberOfTouchesRequired)并且触摸不超出允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan).
当你设置minimumPressDuration为3秒之类的高allowableMovement点和1像素之类的低点时,这就变得很明显了.如果你的手指完全滚动,手势将无法开始.但是如果你设置allowableMovement为100,你的手指可以滚动很多,手势就会开始.
这样就像其他属性一样.它们都是关于手势开始所需要的.
我以为的确allowableMovement有这个目的.我用一个小的子类来实现"预期的"行为allowableMovementAfterBegan.
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface TDMLongPressGestureRecognizer : UILongPressGestureRecognizer
@property (nonatomic, assign) CGFloat allowableMovementAfterBegan;
@end
@implementation TDMLongPressGestureRecognizer
{
CGPoint initialPoint;
}
- (instancetype)initWithTarget:(id)target action:(SEL)action
{
self = [super initWithTarget:target action:action];
if (self) {
// Use default value for allowableMovement before touches begin
_allowableMovementAfterBegan = self.allowableMovement;
}
return self;
}
- (void)reset
{
[super reset];
initialPoint = CGPointZero;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
initialPoint = [self locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if (!CGPointEqualToPoint(initialPoint, CGPointZero))
{
CGPoint currentPoint = [self locationInView:self.view];
CGFloat distance = hypot(initialPoint.x - currentPoint.x, initialPoint.y - currentPoint.y);
if (distance > self.allowableMovementAfterBegan)
self.state = UIGestureRecognizerStateFailed;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2311 次 |
| 最近记录: |