当压下时,UILongPressGestureRecognizer被调用两次。

时间:2022-04-19 02:11:22

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

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

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?

当我向下按压超过2秒时,文本“double oo”会被打印两次。这是为什么呢?我怎样才能解决呢?

7 个解决方案

#1


617  

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:

uilongpressgesturerecnizer是一种连续事件识别器。你必须观察状态,看看这是事件的开始、中间还是结束,然后采取相应的行动。也就是说,你可以在开始后扔掉所有的事件,或者只看你需要的动作。从类参考:

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.

长按手势是连续的。手势开始(uigesturerecnizerstatebegan),当指定周期内按下允许的手指数(需要的触摸数)时(最小预置),触摸不会超出允许的移动范围(允许的移动)。手势识别器在手指移动时转换为改变状态,当任何一个手指被抬起时结束(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
     }
  }

#2


112  

To check the state of the UILongPressGestureRecognizer just add an if statement on the selector method:

要检查UILongPressGestureRecognizer的状态,只需在选择器方法上添加一个if语句:

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender {    
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    } else if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Long press detected.");
    }
}

#3


67  

You need to check the correct state, since there are different behaviors for each state. Most likely you're going to need the UIGestureRecognizerStateBegan state with the UILongPressGestureRecognizer.

您需要检查正确的状态,因为每个状态都有不同的行为。很可能你需要uilongpressgesturerecnizerstatebegan状态。

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

...

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
    if(UIGestureRecognizerStateBegan == gesture.state) {
        // Called on start of gesture, do work here
    }

    if(UIGestureRecognizerStateChanged == gesture.state) {
        // Do repeated work here (repeats continuously) while finger is down
    }

    if(UIGestureRecognizerStateEnded == gesture.state) {
        // Do end work here when finger is lifted
    }
}

#4


16  

Just try this:

Objective-C

objective - c

- (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    } else if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Long press detected.");
    }
}

Swift 2.2:

斯威夫特2.2:

func handleLongPress(sender:UILongPressGestureRecognizer) {

        if (sender.state == UIGestureRecognizerState.Ended) {
            print("Long press Ended");
        } else if (sender.state == UIGestureRecognizerState.Began) {
            print("Long press detected.");
        }
}

#5


11  

Here's how to handle it in Swift:

以下是如何快速处理的方法:

func longPress(sender:UILongPressGestureRecognizer!) {

        if (sender.state == UIGestureRecognizerState.Ended) {
            println("Long press Ended");
        } else if (sender.state == UIGestureRecognizerState.Began) {
            println("Long press detected.");
        }
}

#6


11  

Swift 3.0:

斯威夫特3.0:

func handleLongPress(sender: UILongPressGestureRecognizer) {

    if sender.state == .ended {
        print("Long press Ended")
    } else if sender.state == .began {
        print("Long press detected")
    }

#7


5  

your gesture handler receives call for each state of gesture. so you need to put a check for each state and put your code in required state.

您的手势处理程序接收每个手势状态的调用。因此,您需要对每个状态进行检查,并将代码置于必需状态。

One must prefer using switch-case over if-else :

一个人必须更喜欢使用开关箱,而不是if-else:

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

-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
        switch(gesture.state){
          case UIGestureRecognizerStateBegan:
               NSLog(@"State Began");
               break;
          case UIGestureRecognizerStateChanged:
               NSLog(@"State changed");
               break;
          case UIGestureRecognizerStateEnded:
               NSLog(@"State End");
               break;
          default:
               break;
         }
}

#1


617  

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:

uilongpressgesturerecnizer是一种连续事件识别器。你必须观察状态,看看这是事件的开始、中间还是结束,然后采取相应的行动。也就是说,你可以在开始后扔掉所有的事件,或者只看你需要的动作。从类参考:

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.

长按手势是连续的。手势开始(uigesturerecnizerstatebegan),当指定周期内按下允许的手指数(需要的触摸数)时(最小预置),触摸不会超出允许的移动范围(允许的移动)。手势识别器在手指移动时转换为改变状态,当任何一个手指被抬起时结束(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
     }
  }

#2


112  

To check the state of the UILongPressGestureRecognizer just add an if statement on the selector method:

要检查UILongPressGestureRecognizer的状态,只需在选择器方法上添加一个if语句:

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender {    
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    } else if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Long press detected.");
    }
}

#3


67  

You need to check the correct state, since there are different behaviors for each state. Most likely you're going to need the UIGestureRecognizerStateBegan state with the UILongPressGestureRecognizer.

您需要检查正确的状态,因为每个状态都有不同的行为。很可能你需要uilongpressgesturerecnizerstatebegan状态。

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

...

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
    if(UIGestureRecognizerStateBegan == gesture.state) {
        // Called on start of gesture, do work here
    }

    if(UIGestureRecognizerStateChanged == gesture.state) {
        // Do repeated work here (repeats continuously) while finger is down
    }

    if(UIGestureRecognizerStateEnded == gesture.state) {
        // Do end work here when finger is lifted
    }
}

#4


16  

Just try this:

Objective-C

objective - c

- (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Long press Ended");
    } else if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"Long press detected.");
    }
}

Swift 2.2:

斯威夫特2.2:

func handleLongPress(sender:UILongPressGestureRecognizer) {

        if (sender.state == UIGestureRecognizerState.Ended) {
            print("Long press Ended");
        } else if (sender.state == UIGestureRecognizerState.Began) {
            print("Long press detected.");
        }
}

#5


11  

Here's how to handle it in Swift:

以下是如何快速处理的方法:

func longPress(sender:UILongPressGestureRecognizer!) {

        if (sender.state == UIGestureRecognizerState.Ended) {
            println("Long press Ended");
        } else if (sender.state == UIGestureRecognizerState.Began) {
            println("Long press detected.");
        }
}

#6


11  

Swift 3.0:

斯威夫特3.0:

func handleLongPress(sender: UILongPressGestureRecognizer) {

    if sender.state == .ended {
        print("Long press Ended")
    } else if sender.state == .began {
        print("Long press detected")
    }

#7


5  

your gesture handler receives call for each state of gesture. so you need to put a check for each state and put your code in required state.

您的手势处理程序接收每个手势状态的调用。因此,您需要对每个状态进行检查,并将代码置于必需状态。

One must prefer using switch-case over if-else :

一个人必须更喜欢使用开关箱,而不是if-else:

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

-(void)handleLongPress:(UILongPressGestureRecognizer *)gesture {
        switch(gesture.state){
          case UIGestureRecognizerStateBegan:
               NSLog(@"State Began");
               break;
          case UIGestureRecognizerStateChanged:
               NSLog(@"State changed");
               break;
          case UIGestureRecognizerStateEnded:
               NSLog(@"State End");
               break;
          default:
               break;
         }
}