【问题标题】:UIPanGestureRecognizer on UITableViewCell overrides UITableView's scroll view gesture recognizerUITableViewCell 上的 UIPanGestureRecognizer 覆盖 UITableView 的滚动视图手势识别器
【发布时间】:2012-04-17 00:38:35
【问题描述】:

我已将UITableViewCell 子类化,并在该类中应用了平移手势识别器:

UIPanGestureRecognizer *panning = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanning:)];
panning.minimumNumberOfTouches = 1;
panning.maximumNumberOfTouches = 1;
[self.contentView addGestureRecognizer:panning];
[panning release];

然后我实现了应该允许在表格视图中同时进行手势的委托协议:

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

然后我在 handlePanning 方法中放置一个日志,只是为了查看它何时被检测到:

- (void)handlePanning:(UIPanGestureRecognizer *)sender {
    NSLog(@"PAN");
}

我的问题是我无法垂直滚动表格视图中的单元格列表,并且无论我平移哪个方向都会调用handlePanning

我想要的是 handlePanning 仅在只有水平平移而不是垂直平移时才被调用。希望得到一些指导。

【问题讨论】:

    标签: ios cocoa-touch uigesturerecognizer


    【解决方案1】:

    您是否尝试过设置pannings 委托属性?

    panning.delegate = /* class name with the delegate method in it */;
    

    您还需要使该类符合 UIGestureRecognizerDelegate。

    【讨论】:

      【解决方案2】:

      子类化平移手势识别器并使其仅识别水平平移。有一个很棒的 WWDC 2010 视频,介绍了可用的自定义手势识别器问题。实际上有两个关于该主题的,请查看https://developer.apple.com/videos/archive/

      • 使用手势识别器简化触摸事件处理
      • 高级手势识别

      【讨论】:

        【解决方案3】:

        在 tableview 上添加手势识别器。从中,您可以获得单元格对象。从那里您可以处理单元格功能。对于每个手势,都会有一个开始、更改和结束状态。因此,存储开始位置。

            CGPoint beginLocation = [gesture locationInView:tblView]; // touch begin state.
        
            CGPoint endLocation = [gesture locationInView:tblView]; // touch end state.
        

        利用这一点,可以得到IndexPath

            NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:beginPoint];
        

        从此索引路径,您可以访问单元格。

                    UITableViewCell *cell = [tableview cellForRowAtIndexPath : indexPath];
        

        使用这个 Cell 对象,您可以处理它。

        【讨论】:

        • "indexPathForRowAtPoint" - 谢谢,正是我找的
        【解决方案4】:

        您是否尝试过将bounces 属性设置为NO?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-04-21
          • 1970-01-01
          • 2015-06-05
          • 2013-09-28
          • 2012-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多