DoubleTap recognition in UITableView

I have setup a pair of UITapGestureRecognizers to try to catch single and double taps on a UITableView. When the tap handler is called, the tap view member is the correct UITableView. But when I try to get the locationInView point it returns what appears to be nonesensical values. The X value is -1100 and the inital Y value is -320. These values are returned for the first 4-5 rows. On about the 6th table row the CGPoint's Y value will start incrementing. Passing the CGPoint to [tableView indexPathForRowAtPoint:tapPoint] returns a nil value. Changing the value of the UIView in locationInView to nil causes it to return 0,0 for all rows. Inside the tap handler calling [tableView indexPathForSelectedRow] returns a nil as well. Taking out the UITapGestureRecognizers causes the tableview to work just fine and when didSelectRowAtIndexPath is called with the proper index.


The exact same code works correctly on the iPad and returns sensible numbers for locationInView and [tableView indexPathForRowAtPoint:tapPoint] returns the correct index.


Any ideas on a non-fragile workaround to catch a double tap?


Pretty simple code:



@Property (strong) IBOutlet UITableView       *movieTableList;

.....

-(void)singleTap:(UITapGestureRecognizer *)tap
{
  NSIndexPath *indexPath;
  CGPoint     tapPoint;

  if ([tap state] == UIGestureRecognizerStateEnded) {

    tapPoint = [tap locationInView:_movieTableList];
    DebugLog(@"%.2f %.2f", tapPoint.x, tapPoint.y);

    indexPath = [_movieTableList indexPathForRowAtPoint:tapPoint];

    }
}

.....

  UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
  [doubleTap setNumberOfTapsRequired:2];
  [_movieTableList addGestureRecognizer:doubleTap];

  UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
  [singleTap setNumberOfTapsRequired:1];
  [singleTap requireGestureRecognizerToFail:doubleTap];
  [_movieTableList addGestureRecognizer:singleTap];
Answered by Engineer in 140894022

When using

UIGestureRecognizer
with presses instead of touches, the location in the view probably isn't the best way to determine what was tapped. Instead, you should probably check what view is currently focused using
[[UIScreen mainScreen] focusedView]


The view you get back from that ought to be one of the table view cells, which you can then pass to your table view's

indexPathForCell:
to find out which index path was tapped or double-tapped.
Accepted Answer

When using

UIGestureRecognizer
with presses instead of touches, the location in the view probably isn't the best way to determine what was tapped. Instead, you should probably check what view is currently focused using
[[UIScreen mainScreen] focusedView]


The view you get back from that ought to be one of the table view cells, which you can then pass to your table view's

indexPathForCell:
to find out which index path was tapped or double-tapped.

Thanks Justin - that worked perfectly and makes perfect sense. Face palm.

01161631

😊01161631

DoubleTap recognition in UITableView
 
 
Q