How can I tell UIGestureRecognizer to cancel an existing bar?

I have a UIPanGestureRecognizer that I use to track an object ( UIImageView ) under the user's finger. I only care about moving along the X axis, and if the touch dodges above or below the object frame on the Y axis, I want to finish the touch.

I have everything I need to determine if the touch object is in object Y, but I donโ€™t know how to cancel the touch event. Reflect recognizer property cancelsTouchesInView doesn't seem to do what I want.

Thank!

+44
cocoa-touch uigesturerecognizer
Oct. 14 '10 at 21:43
source share
6 answers

This little trick works for me.

 @implementation UIGestureRecognizer (Cancel) - (void)cancel { self.enabled = NO; self.enabled = YES; } @end 

From the UIGestureRecognizer @enabled documentation:

Disables gesture recognizers so that it does not receive touches. The default value is YES. If you change this property to NO, and the gesture recognizer currently recognizes the gesture, the gesture recognizer enters the cancel state.

+136
Nov 12 '10 at 17:38
source share

@matej answer in Swift.

 extension UIGestureRecognizer { func cancel() { isEnabled = false isEnabled = true } } 
+5
Apr 08 '16 at 7:15
source share

Obj-C:

 recognizer.enabled = NO; recognizer.enabled = YES; 

Swift 3:

 recognizer.isEnabled = false recognizer.isEnabled = true 
+4
Apr 20 '16 at 18:28
source share

How about this from apple docs :

 @property(nonatomic, getter=isEnabled) BOOL enabled 

Disables gesture recognizers so that it does not receive strokes. The default value is YES. If you change this property to NO while the gesture recognizer is currently recognizing the gesture, the gesture recognizer goes into the canceled state.

+3
May 15 '12 at 12:56
source share

You have several ways to handle this:

  • If you are writing a custom subclass of the recognizer gestures, you can easily do this by calling -ignoreTouch:withEvent: from inside the recognizer when you notice that it deviates from the area you care about.

  • Since you are using a standard panorama recognizer, and the touch starts with OK (so you do not want to interfere with its delegate functions), you can really make your difference only when you receive the target actions recognizer. Check the Y value of the returned translationInView: or locationInView: return values โ€‹โ€‹and locationInView: it accordingly.

+1
Aug 15 2018-11-11T00:
source share

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

0
Oct 12 '17 at 17:25
source share



All Articles