我的UILongPressGestureRecognizer
中的allowableMovement
属性似乎被忽略。我使用Single View Application模板创建了一个新项目(Xcode 4.5.1,iOS 6),并在视图中添加了长按手势识别器。有一个连接线和一个动作。下面是操作方法的代码:allowableMovement似乎被忽略
- (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);
}
如果我按下足够长的时间放手我在日志中得到这样的:
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
这是我所期望的那样。
但无论我将allowableMovement
设置为(正面,负面,大,小),一旦状态为UIGestureRecognizerStateBegan
,我就可以将手指拖到屏幕上。状态更改为UIGestureRecognizerStateChanged
,并且频繁更新,并且locationInView继续准确跟踪。当我放手的时候,我得到了UIGestureRecognizerStateRecognized
状态,并且最终输出到了日志中。
该类参考指出,如果移动超过allowableMovement
,识别器将失败。为什么allowableMovement
属性似乎被忽略?
@ Murray Sagal:这也解决了我的疑惑。想知道,如果有一种方法可以检测用户是否在LongPressGesture开始后将他们的手指拖出视图。 – Kashif
@TPOS我认为你可以使用'locationInView:'或其中一种方法。但它听起来像是值得自己的问题。 –