Two UILongPressGestureRecognizer, you can shoot, others can not

guys,

I added two UILongPressGestureRecognizer. I want the two buttons to be pressed within 0.3 seconds, run the "shortPressHandler". If the user continues to press these two buttons for another 1.2 seconds, run "longPressHandler". Now I get shortPressHandler, and longPressHandler never started. I think this can happen because shortPressGesture is recognized first and longPressGesture never gets a chance. Can someone show me how to achieve what I want? Thanks in advance.

UILongPressGestureRecognizer *longPressGesture =[[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)] autorelease]; longPressGesture.numberOfTouchesRequired = 2; longPressGesture.minimumPressDuration = 1.5; longPressGesture.allowableMovement = 10; longPressGesture.cancelsTouchesInView = NO; longPressGesture.enabled = true; [self.view addGestureRecognizer:longPressGesture]; UILongPressGestureRecognizer *shortPressGesture =[[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(shortPressHandler:)] autorelease]; shortPressGesture.numberOfTouchesRequired = 2; shortPressGesture.minimumPressDuration = 0.3; shortPressGesture.allowableMovement = 10; shortPressGesture.cancelsTouchesInView = NO; shortPressGesture.enabled = true; [self.view addGestureRecognizer:shortPressGesture]; 
+6
source share
1 answer

Insert this line before adding shortPressGesture:

  [shortPressGesture requireGestureRecognizerToFail:longPressGesture]; 

Note: shortGesture will not be called immediately after a 0.3 second hold, but when the rollback is released if it ranged from 0.3 second to 1.2 second. If the crane is longer than 1.2 s (you have 1.5 s in your code, which is probably a typo), then only longPressGesture will start.

EDIT:

However, if you want event handlers to fire (in the case of a long press), you must do this:

Your UIView should implement the <UIGestureRecognizerDelegate> file in .h .

In the .m file, you add this method:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } 

Now instead of adding a line:

 [shortPressGesture requireGestureRecognizerToFail:longPressGesture]; 

You add two lines:

 shortPressGesture.delegate = self; longPressGesture.delegate = self; 

NOTE. If you have another UIGestureRecognisers associated with your UIView , you will need to add some validation to shouldRecognizeSimultaneouslyWithGestureRecognizer: otherwise you can just return YES .

+6
source

Source: https://habr.com/ru/post/910791/


All Articles