简体   繁体   中英

How to detect double tap / touch event in tableview

After a long but all in vain search, I am unable to detect the double tap / touch event in my tableview , actually want to call the detail view on double tap on any TableViewCell and in reality I don't even know how to do it at all .

This is my code so far…

In viewDidLoad

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.myTable addGestureRecognizer:tapGesture];

the handleTapGesture method is

- (void)handleTapGesture:(UITapGestureRecognizer *)sender {
  if (sender.state == UIGestureRecognizerStateRecognized) {
      flag = true;
  }
}

and finally on touching or tapping the cell of tableview the delegate method is

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (flag == true)
    {
        DetailInvoicing *detail = [[DetailInvoicing alloc] initWithNibName:@"DetailInvoicing" bundle:nil];
        detail.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        detail.customerName = [customerArray objectAtIndex:indexPath.row];
        [self presentViewController:detail animated:YES completion:nil];
    }
}

If I remove this flag condition new view is called on just single touch. where am I wrong or is there any other way to do it .

There is a simple method, without using UITapGestureRecognizer nor implementing custom table view class.

@implementation MyTableViewController
NSTimeInterval lastClick;
NSIndexPath *lastIndexPath;

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSTimeInterval now = [[[NSDate alloc] init] timeIntervalSince1970];
    if ((now - lastClick < 0.3) && [indexPath isEqual:lastIndexPath]) {
        // Double tap here
        NSLog(@"Double Tap!");
    }
    lastClick = now;
    lastIndexPath = indexPath;
}
@end

It is just view lines of code.

Corrected For Swift 3.1

var lastClick: TimeInterval
var lastIndexPath: IndexPath? = nil

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let now: TimeInterval = Date().timeIntervalSince1970
    if (now - lastClick < 0.3) &&
        (lastIndexPath?.row == indexPath.row ) {
         print("Double Tap!")
    }
    lastClick = now
    lastIndexPath = indexPath
}

Try like this, in your customtableview class implement this below method:-

 - (void)touchesEnded:(NSSet *)touches 
    withEvent:(UIEvent *)event {
    UITouch *aTouch = [touches anyObject];
  // below numberofclick in integer type
   self.numberOfClicks = [aTouch tapCount];
 [super touchesEnded:touches withEvent:event];
 }

Now in your tableview delegate method (didSelectRowAtIndexPath). And then detect the numberOfclicks whether it is 2 or 1 and modified your condition accordingly. Below is the line of code which you can put in your delegate method to detect the number of taps pressed

   if (myCell.numberOfClicks == 2) {
   NSLog(@"Double clicked");   }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM