這裡我們為tableview添加長按手勢
UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPressGr.minimumPressDuration = 0.5f;
longPressGr.numberOfTouchesRequired = 1;
[_tableView addGestureRecognizer:longPressGr];
[longPressGr release];
這時我們會發現每次按住tableView並且松開的時候, longPressAction: 這個方法會執行2次
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
CGPoint point = [longPress locationInView:_tableView];
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:point]; // 可以獲取我們在哪個cell上長按
if (indexPath != nil) {
NSLog(@"%ld", indexPath.row);
}
}
}