【问题标题】:UILongPressGestureRecognizer not works with tableviewUILongPressGestureRecognizer 不适用于 tableview
【发布时间】:2012-08-06 08:14:52
【问题描述】:

我在我的 ViewDidLoad 方法中添加了一个 UILongPressGestureRecognizer 到一个表视图中。我添加了这个来检测代码中表格视图上的长按。但它永远不会奏效。在 ViewDidLoad 我添加了这段代码:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
                                      initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.resultTableView addGestureRecognizer:lpgr];
[lpgr release];

我也添加了这个方法:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.resultTableView];

    NSIndexPath *indexPath = [self.resultTableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {

        NSLog(@"long press on table view but not on a row");
    }
    else {


        NSLog(@"long press on table view at row %d", indexPath.row);
    }


}

请帮我解决这个问题?

【问题讨论】:

    标签: objective-c ios xcode uitableview ios5


    【解决方案1】:

    您的代码正在运行。我认为您必须在 .h 文件中添加 UIGestureRecognizerDelegate 委托或如何声明 resultTableView 我的意思是您以编程方式定义或使用 .xib 文件。检查一次。

    我试过这样。

         resultTableView = [[UITableView alloc] init];
         resultTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420) style:UITableViewStylePlain];
        resultTableView.rowHeight = 100.0;
        resultTableView.delegate=self;
         resultTableView.dataSource=self;
        [self.view addSubview:resultTableView];
    
        UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
                                              initWithTarget:self action:@selector(handleLongPress:)];
        lpgr.minimumPressDuration = 2.0; //seconds
        lpgr.delegate = self;
        [resultTableView addGestureRecognizer:lpgr];
        [lpgr release];
    

    【讨论】:

    • resultTableView 在 xib 文件中并使用 IBoutlet 变量连接
    • @PrasadG 当我尝试添加 tableviewCell 时。不工作。
    • [cell.LongGestureVw addGestureRecognizer:lpgr];
    【解决方案2】:

    您似乎想将手势添加到各个单元格,但您正在将手势添加到表格中。请尝试将手势添加到您的 UITableViewCell

    【讨论】:

    • 感谢您的快速重播。我会试试的。
    • 在tableview上添加手势识别器是最好的方法,也许它与tableview上的pan手势识别器在长按被识别之前触发有关
    • @wattson12:[yourGestureRecognizer requireGestureRecognizerToFail:tableView.panGestureRecognizer] 可以缓解这种情况。
    【解决方案3】:

    如果手势识别器被UITableView panGestureRecognizer 阻止,则实现委托以确保两者都可以工作

     -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多