I want to get a single tap as well as double tap on a UITableViewCell
. I have created a customDataSource for the UITableview
.
How can I achieve this ?
The correct way to do this is to add your UITapGestureRecognizer on the tableView :
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.numberOfTouchesRequired = 1;
[self.tableView addGestureRecognizer:doubleTap];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[singleTap requireGestureRecognizerToFail:doubleTap];
[self.tableView addGestureRecognizer:singleTap];
Then in your callback methods you find the cell with something like this :
-(void)singleTap:(UITapGestureRecognizer*)tap
{
if (UIGestureRecognizerStateEnded == tap.state)
{
CGPoint p = [tap locationInView:tap.view];
NSIndexPath* indexPath = [_tableView indexPathForRowAtPoint:p];
UITableViewCell* cell = [_tableView cellForRowAtIndexPath:indexPath];
// Do your stuff
}
}
I have achieved this by adding the following to your gesture recognizer, although I find that there's a slight delay when single tapping.
[tapRecognizer setDelaysTouchesBegan:YES];
[tapRecognizer setCancelsTouchesInView:YES];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With