我想在 UITableViewCell
上长按以打印“快速访问菜单”。
有人已经这样做了吗?
特别是 UITableView
上的手势识别?
最佳答案
首先将长按手势识别器添加到表格 View :
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.myTableView addGestureRecognizer:lpgr];
[lpgr release];
然后在手势处理程序中:
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
CGPoint p = [gestureRecognizer locationInView:self.myTableView];
NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:p];
if (indexPath == nil) {
NSLog(@"long press on table view but not on a row");
} else if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"long press on table view at row %ld", indexPath.row);
} else {
NSLog(@"gestureRecognizer.state = %ld", gestureRecognizer.state);
}
}
你必须小心,这样它就不会干扰用户正常点击单元格,还要注意 handleLongPress
可能会触发多次(这将是由于手势识别器状态变化)。
关于ios - 长按 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3924446/