Solution to UIGestureRecognizer error after recognition?

I have a question that may seem basic, but it cannot understand.

The main question: how to programmatically put gesturerecognizer into a failed state from a handler, whereas in a UIGestureRecognizerStateBegan or UIGestureRecognizerStateChanged?

More detailed explanation: I have a long gesture recognition id for a UIView inside a UIScrollView. I did

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

because otherwise I can't get the scroll to scroll as soon as the user places his finger down in the view. This is a basic touch like a safari, where you hold your finger down the link that selects the link, but scrolls up or down - then the link is not highlighted, and the scroll is viewed.

I can make this basically work right now, since both gestures are recognized, but it would be better if I could detect movement in the longpress gesturerecognizer StateChanged, and if it has more than 20 pixels or so, just programmatically make longpress fail.

Can this be done? Or am I digging in the wrong place?

+6
source share
3 answers

Another question that I found right after I posted the question.

Here is what I do in the gesture recognizer handler:

 else if (sender.state == UIGestureRecognizerStateChanged) { CGPoint newTouchPoint = [sender locationInView:[self superview]]; CGFloat dx = newTouchPoint.x - initTouchPoint.x; CGFloat dy = newTouchPoint.y - initTouchPoint.y; if (sqrt(dx*dx + dy*dy) > 25.0) { sender.enabled = NO; sender.enabled = YES; } } 

Thus, if the finger moves more than 25 pixels in any direction, enabling the enabled property in NO will cause the recognizer to fail. So this will do what I want!

+4
source

If it is a UILongPressGestureRecognizer , just set its allowableMovement property.

 UILongPressGestureRecognizer* recognizer = [your recognizer]; recognizer.allowableMovement = 25.0f; 
+4
source

According to the documentation, you can subclass a gesture recognizer:

In YourPanGestureRecognizer.m:

 #import "YourPanGestureRecognizer.h" @implementation YourPanGestureRecognizer - (void) cancelGesture { self.state=UIGestureRecognizerStateCancelled; } @end 

In YourPanGestureRecognizer.h:

 #import <UIKit/UIKit.h> #import <UIKit/UIGestureRecognizerSubclass.h> @interface NPPanGestureRecognizer: UIPanGestureRecognizer - (void) cancelGesture; @end 

Now you can call if from outside

 YourPanGestureRecognizer *panRecognizer = [[YourPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMoved:)]; [self.view addGestureRecognizer:panRecognizer]; [...] -(void) panMoved:(YourPanGestureRecognizer*)sender { [sender cancelGesture]; // This will be called twice } 

Link: https://developer.apple.com/documentation/uikit/uigesturerecognizer?language=objc

+1
source

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


All Articles