ios - 长按 UITableView

标签 ios iphone uitableview gesture-recognition long-press

我想在 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/

相关文章:

ios - 快速点击按钮时添加行或文本字段

ios - Eureka 表格 : How to change form global tint colour

ios - 如何通过 VSTS 发布/发布 iOS 应用程序

iphone - EXC_BAD_ACCESS (SIGSEGV) 崩溃

iphone - iOS 开发 : What are some ways I can troubleshoot a lag issue in my game that occurs 15 - 30 minutes after playing it?

iphone - 跨越多个 UITableViewCells 的背景图像(分组)

ios - Xcode 4,基于 View 的应用程序 + UITableView 向上滚动时崩溃

ios - 更改一个 NSManaged 对象会更改其他对象

ios - UISegmentedControl 行为

ios - 来自自定义 Xib 的 TableView 中的动态单元格大小