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!
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.
@matej's answer in Swift.
extension UIGestureRecognizer {
func cancel() {
isEnabled = false
isEnabled = true
}
}