UILongPressGestureRecognizer在按下时调用两次 [英] UILongPressGestureRecognizer gets called twice when pressing down

查看:267
本文介绍了UILongPressGestureRecognizer在按下时调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am detecting if the user has pressed down for 2 seconds:

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秒。为什么是这样?我如何解决?

The text "double oo" gets printed twice when I press down for longer than 2 seconds. Why is this? How can I fix?

推荐答案

UILongPressGestureRecognizer是一个连续事件识别器。你必须查看状态,看看这是事件的开始,中间还是结束,并相应地采取行动。即你可以在开始后丢弃所有事件,或者只根据需要查看运动。从类别参考: / p>

UILongPressGestureRecognizer is a continuous event recognizer. You have to look at the state to see if this is the start, middle or end of the event and act accordingly. i.e. you can throw away all events after the start, or only look at movement as you need. From the Class Reference:


长按手势是连续的。当在指定的时间段(minimumPressDuration)中按下可允许的手指的数量(numberOfTouchesRequired)并且触摸没有移动超过允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan)。

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.

当手指移动时,手势识别器转换到Change状态,并且当手指移动时,手势识别器结束(UIGestureRecognizerStateEnded)现在你可以跟踪像这样的状态

Now You Can Track The State Like This

-  (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
     }
  }

这篇关于UILongPressGestureRecognizer在按下时调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆