Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell a UIGestureRecognizer to cancel an existing touch?

I have a UIPanGestureRecognizer I am using to track an object (UIImageView) below a user's finger. I only care about motion on the X axis, and if the touch strays above or below the object's frame on the Y axis I want to end the touch.

I've got everything I need for determining if a touch is within the object's Y bounds, but I don't know how to cancel the touch event. Flipping the recognizer's cancelsTouchesInView property doesn't seem to do what I want.

Thanks!

like image 802
Josh French Avatar asked Oct 14 '10 21:10

Josh French


2 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 a gesture recognizers so it does not receive touches. The default value is YES. If you change this property to NO while a gesture recognizer is currently recognizing a gesture, the gesture recognizer transitions to a cancelled state.

like image 155
Matej Bukovinski Avatar answered Nov 16 '22 00:11

Matej Bukovinski


@matej's answer in Swift.

extension UIGestureRecognizer {   func cancel() {     isEnabled = false     isEnabled = true   } } 
like image 30
Hlung Avatar answered Nov 16 '22 00:11

Hlung