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

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

问题描述

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

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self动作:@选择器(handleLongPress:)];longPress.minimumPressDuration = 2.0;[self addGestureRecognizer:longPress];[长新闻稿];

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

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

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

解决方案

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

长按手势是连续的.手势开始 (UIGestureRecognizerStateBegan) 当在指定时间段 (minimumPressDuration) 内按下允许的手指数量 (numberOfTouchesRequired) 并且触摸没有超出允许的移动范围 (allowableMovement) 时,手势开始 (UIGestureRecognizerStateBegan).每当手指移动时,手势识别器就会转换到 Change 状态,并在任何手指抬起时结束 (UIGestureRecognizerStateEnded).

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

- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {如果(sender.state == UIGestureRecognizerStateEnded){NSLog(@"UIGestureRecognizerStateEnded");//在手势结束时做任何你想做的事}else if (sender.state == UIGestureRecognizerStateBegan){NSLog(@"UIGestureRecognizerStateBegan.");//做任何你想做的手势开始}}

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];

This is how I handle the long press:

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

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

解决方案

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:

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.

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天全站免登陆