How to remove gesture recogniser
This loop will remove all gesture recognizers a view has
for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers) {
[self.view removeGestureRecognizer:recognizer];
}
From the WWDC 2015, Cocoa Touch Best Practices, it is suggested that you keep a property or iVar if you need to access it later, and don't go with using viewWithTag:
.
Moto: Properties instead of Tags
This saves you from some trouble:
- When dealing with multiple gestures, you remove the gesture that you want directly with accessing the property and remove it. (Without the need to iterate all the view's gestures to get the correct one to be removed)
- Finding the correct gesture by the tag when you are iterating, is very misleading when you have multiple tags on views, and when having conflicts with a specific tag
(i.e) You implemented it first time with tags, and everything works as expected. Later you work on another functionality which lets say breaks this and causes undesired behavior that you don't expect it. Log doesn't give you a warning, and the best thing you can get depending on the case it's a crash signalizing unrecognized selector sent to instance. Sometimes you won't get any of these.
Solution
Declare the iVar
@implementation YourController {
UITapGestureRecognizer *tap;
}
Setup your view
- (void) helpClicked {
//Your customization code
//Adding tap gesture
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissView)];
[self addGestureRecognizer:tap];
}
Remove the gesture directly
- (void) dismissView {
[self.view removeGestureRecognizer:tap];
}