我的 UILongPressGestureRecognizer
中的 allowableMovement
属性似乎被忽略了。我使用单 View 应用程序模板创建了一个新项目(Xcode 4.5.1,iOS 6)并向 View 添加了一个长按手势识别器。有一个 socket 和一个 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);
}
如果我按下足够长的时间然后松开,我会在日志中得到这个:
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
设置为(positive, negative, big, small),一旦状态为 UIGestureRecognizerStateBegan
,我就可以在屏幕上拖动手指.状态变化为UIGestureRecognizerStateChanged
,并有频繁的更新,locationInView继续准确跟踪。当我放手时,我得到 UIGestureRecognizerStateRecognized
状态和日志的最终输出。
类引用指出,如果移动超过 allowableMovement
,识别器就会失败。为什么 allowableMovement
属性似乎被忽略了?
最佳答案
allowableMovement
属性与手势开始后可以拖动多远无关。这是关于在手势开始之前你可以移动多远。
来自类引用:
The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement).
当您将 minimumPressDuration
设置为 3 秒之类的高值并将 allowableMovement
设置为 1 像素之类的低值时,这会变得很明显。如果您的手指滚动,手势将不会开始。但是,如果您将 allowableMovement
设置为 100,您的手指可以滚动很多,手势就会开始。
在这种方式下它就像其他属性一样。它们都是关于手势开始所需的内容。
关于objective-c - allowableMovement 似乎被忽略了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13146412/