ios给cell添加长按手势怎么识别是哪个cell
1个回答
展开全部
这里我们为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);
}
}
}
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);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询