How to disable other touch gestures after adding another tap gesture to the view?
- by Hudson Duan
I have a view with some tables and buttons on it, and then I want to add a tap gesture to the entire view, but I only want that gesture recognizer to recognize taps.
Ideally, I want to do something when the added gesture recognizer is tapped, then remove that gesture recognizer after so the other buttons and tables can be accessed. Basically a tap to dismiss functionality that replicates something like the facebook notifications window, tap outside to dismiss, but not interfere with the buttons outside of the notifications view.
Can anybody help?
My current code is:
NotificationsWindow *customView = [[[NSBundle mainBundle]loadNibNamed:@"NotificationsWindow" owner:self options:nil]objectAtIndex:0];
customView.frame= CGRectMake(12, 12, customView.frame.size.width, customView.frame.size.height);
UITapGestureRecognizer *recognizerForSubView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehindAgain:)];
[recognizerForSubView setNumberOfTapsRequired:1];
recognizerForSubView.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
[customView addGestureRecognizer:recognizerForSubView];
[self.view addSubview:customView];
UITapGestureRecognizer *recognizerForSuperView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
[recognizerForSuperView setNumberOfTapsRequired:1];
recognizerForSuperView.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
[customView.superview addGestureRecognizer:recognizerForSuperView];
(void)handleTapBehind:(UITapGestureRecognizer *)sender
{
NSLog(@"tapped");
[[self.view.subviews lastObject] removeFromSuperview];
[self.view removeGestureRecognizer:sender];
}
I want to make it so that the recognizer for the super view dismisses the subview, but not to interfere with the other taps on the super view.