Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 374
M Zubair Shamshad Avatar asked Mar 15 '14 12:03

M Zubair Shamshad


People also ask

How to add double tap gesture in ios?

Go to Settings > Accessibility > Touch, and tap Back Tap. Tap Double Tap or Triple Tap and choose an action. Double or triple tap on the back of your iPhone to trigger the action you set.

How to add double tap gesture in Swift?

viewTop. addGestureRecognizer(singleTap) // Double Tap let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PostDetailViewController. handleDoubleTap)) doubleTap. numberOfTapsRequired = 2 self.

What is Indexpath in Tableview Swift?

Swift version: 5.6. Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.


2 Answers

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.

like image 76
mergenchik Avatar answered Oct 29 '22 21:10

mergenchik


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
}
like image 36
Ali Pishvaee Avatar answered Oct 29 '22 21:10

Ali Pishvaee