UITableViewCell上的UIPanGestureRecognizer会覆盖UITableView的滚动视图手势识别器

时间:2012-04-17 00:38:35

标签: ios cocoa-touch uigesturerecognizer

我已经将UITableViewCell子类化了,在那个课程中我应用了一个Pan手势识别器:

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");
}

我的问题是我无法垂直滚动查看tableview中的单元格列表,无论我向哪个方向移动,都会调用handlePanning

我想要的是handlePanning仅在水平平移而非垂直时才被调用。会很感激一些指导。

4 个答案:

答案 0 :(得分:17)

您是否尝试过设置pannings委托资产?

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

您还需要将该类符合UIGestureRecognizerDelegate。

答案 1 :(得分:3)

对平移手势识别器进行子类化,并使其仅识别水平平移。有关自定义手势识别器问题的WWDC视频很棒。实际上有两个关于那个主题,检查出来。

Simplifying Touch Event Handling with Gesture Recognizers

Advanced Gesture Recognition

答案 2 :(得分:2)

在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对象,您可以处理它。

答案 3 :(得分:-1)

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

相关问题