我正在尝试检测 UITableView 内的触摸和按住手势,同时跟踪选择了哪个单元格。我需要能够区分单元格上的正常点击和持续时间超过 X 秒(可能是 1 秒左右)的触摸。主要的挑战是我想在不继承 UITableViewCell 的情况下执行此操作,因为这样做会显着减慢我的滚动速度。我认为可能有一种使用 NSTimer 来执行此操作的方法,但我似乎无法使其正常工作。使用带有计时器的 touchesBegan: 和 touchesEnded: 已经结束,因为我没有看到任何方法来跟踪选择了哪个单元格,除非有一些方法可以使用这些方法来做到这一点?任何帮助将不胜感激。
提前致谢。
最佳答案
如果我们谈论的是单元格,您可能想要获取已按下的单元格的 indexPath。
在分配新实例后立即将手势识别器添加到单元格:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[cell addGestureRecognizer:longPress];
[longPress release];
处理长按事件,获取indexPath:
-(void) handleLongPress: (UIGestureRecognizer *)longPress {
if (longPress.state==UIGestureRecognizerStateBegan) {
CGPoint pressPoint = [longPress locationInView:table];
NSIndexPath *indexPath = [table indexPathForRowAtPoint:pressPoint];
}
}
https://stackoverflow.com/questions/4146824/