Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tap gesture on containing view blocks tableView selection

UITapGestureRecognizer *singleFingerTap =
            [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:singleFingerTap];
}

- (void)handleSingleTap:(id)handleSingleTap {
    [self.view endEditing:YES];
}

I have a view that contains a tableview, and I want to end editing on textfields when the view is tapped. However, I do not want this to prevent selection of contained elements. Specifically, I have a tableview whose cells cannot be selected unless I swipe them. A single tap gets swallowed by the gesture recognizer and doesn't go through.

If I remove the gesture recognizer, the table works fine.

How can I handle tap events on the view and not prevent subviews from receiving the events?

like image 250
Stefan Kendall Avatar asked Nov 20 '25 04:11

Stefan Kendall


1 Answers

Set your class as the delegate of your UITapGestureRecognizer and implement the method - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch and then check the UITouch location, if is on the tableView then return NO, if not return YES.

For UITouch location use locationInView: method

like image 51
danypata Avatar answered Nov 22 '25 17:11

danypata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!