给UITableView 添加长按手势,识别长按哪一行。
长按手势类UILongPressGestureRecognizer, 属性minimumPressDuration表示最短长按的时间
添加手势代码:
UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
longPressGr.minimumPressDuration = 1.0;
[self.tableView addGestureRecognizer:longPressGr];
[longPressGr release];
响应长按事件代码:
-(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan)
{
CGPoint point = [gesture locationInView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
if(indexPath == nil) return ;
//add your code here
}
}
来源:oschina
链接:https://my.oschina.net/u/257703/blog/144724