我想长按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可能会触发多次(这是由于手势识别器状态更改所致)。

08-26 22:07