Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Tap on UITableViewCell

Tags:

uitableview

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 ?

like image 980
suyash nene Avatar asked Nov 29 '11 07:11

suyash nene


2 Answers

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
    }
}
like image 100
Nyx0uf Avatar answered Sep 17 '22 11:09

Nyx0uf


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];
like image 29
Ngoan Nguyen Avatar answered Sep 17 '22 11:09

Ngoan Nguyen