Stefan Kendall
Stefan Kendall

Reputation: 67842

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?

Upvotes: 3

Views: 2433

Answers (1)

danypata
danypata

Reputation: 10175

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

Upvotes: 4

Related Questions