iOS 中长按手势回调会被触发过两次

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.

根据苹果的文档,这个手势对应的 action 会被调用两次。

Long-press gestures are continuous gestures, meaning that your action method may be called multiple times as the state changes.

正确的做法是在 action 中判断手势的状态,如下:

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

参考

posted on 2017-10-21 10:07  花老🐯  阅读(576)  评论(0编辑  收藏  举报

导航