iphone - 按下时 UILongPressGestureRecognizer 被调用两次

标签 iphone objective-c cocoa-touch gesture-recognition uigesturerecognizer

我正在检测用户是否按下了 2 秒:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                             initWithTarget:self 
                                             action:@selector(handleLongPress:)];
        longPress.minimumPressDuration = 2.0;
        [self addGestureRecognizer:longPress];
        [longPress release];

这就是我处理长按的方式:

-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{
    NSLog(@"double oo");
}

当我按下超过 2 秒时,文本“double oo”会打印两次。为什么是这样?我该如何解决?

最佳答案

UILongPressGestureRecognizer 是一个连续事件识别器。您必须查看状态,看看这是事件的开始、中间还是结束,并采取相应的行动。即您可以在开始后丢弃所有事件,或者仅根据需要查看运动。来自 Class Reference :

Long-press gestures are continuous. 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). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.

现在您可以像这样跟踪状态

-  (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {
      NSLog(@"UIGestureRecognizerStateEnded");
    //Do Whatever You want on End of Gesture
     }
    else if (sender.state == UIGestureRecognizerStateBegan){
       NSLog(@"UIGestureRecognizerStateBegan.");
   //Do Whatever You want on Began of Gesture
     }
  }

关于iphone - 按下时 UILongPressGestureRecognizer 被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3319591/

相关文章:

iphone - - [NSUserDefaults 同步] iOS 上的性能问题

iphone - 限制 UIPanGestureRecognizer 的平移范围

iOS8 Today View Extension 小部件在应用商店中出现 "FairPlay decryption failed"错误

ios - Reactive Cocoa 中的可取消超时

objective-c - UIButton 调用选择器会使应用程序崩溃

iphone - Facebook iOS SDK 注销

iphone - 在iPhone上创建ADTS框架...问题

iphone - 如何在应用商店中找到应用下载日期?

iphone - 将 UIImage 设置为 UIBarButton 项目

iphone - 通过 UIView 更改 MaskedLayer 的大小