您的位置:首页 > 产品设计 > UI/UE

iOS UITableView添加长按事件 —— HERO博客

2016-07-20 18:08 337 查看
为UITableView添加长按事件,长按cell时获取到当前位置:

1. 为UITableView添加长按手势识别器

//添加长按事件
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPressTableViewCell:)];
gesture.minimumPressDuration = 1.0;
[self.tableView addGestureRecognizer:gesture];


2. 实现长按事件方法

//tableViewCell长按事件
- (void)didLongPressTableViewCell:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateBegan) {
//获取到点击的位置
CGPoint point = [gesture locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
if(indexPath == nil) return;

//do something...
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息