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];
[self catchTapForView:customView.superview];
(void)handleTapBehind:(UITapGestureRecognizer *)sender
{
NSLog(@"tapped");
[[self.view.subviews lastObject] removeFromSuperview];
[self.view removeGestureRecognizer:sender];
}
(void)dismissButton:(UIButton *)button {
[button removeFromSuperview];
[[self.view.subviews lastObject] removeFromSuperview];
}
(void)catchTapForView:(UIView *)view {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = view.bounds;
[button addTarget:self action:@selector(dismissButton:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
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.
The easiest way to do what you describe is to overlay a transparent UIView that captures the touch. Simply remove it when touched.
You can use the following code:
Call
catchTapForViewon your view. Do any additional handling indismissButton.