本文介绍了在UIScrollView中向标准Pan Gesture Recognizer添加功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试跟踪手指在 UIScrollView 中的位置。
我有 UIScrollView (见下文),但不幸的是我添加的手势识别器覆盖了标准的手势识别器。

I am trying to keep track of where a finger is in a UIScrollView.I have subclassed UIScrollView (see below) but unfortunately the gesture recognizer that I am adding is overriding the standard one.

结果我得到 NSLog(@Pan)可以工作,但不幸的是视图不再滚动了。

As a result I get NSLog(@"Pan") to work but unfortunately the view doesn't scroll anymore.

如何让两个手势识别器同时工作?

How can I get both gestures recognizers to work at the same time?

谢谢。

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad:animated];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [scrollView addGestureRecognizer:panRecognizer];
}


- (void)pan:(id)sender {
    NSLog(@"Pan");
}


推荐答案

编辑:这种方法有效!您只需要尽快设置 canCancelContentTouches (我在 viewDidLoad 中设置)。

EDIT: this method works! You just need to set canCancelContentTouches as soon as possible (I do it in viewDidLoad).

原始答案:我尝试了一种新的方法,但不幸的是它没有完全发挥作用。

ORIGINAL ANSWER: I have tried a new approach but unfortunately it doesn't fully work.

添加手势识别器我正在继承 UIScrollView 并编写我自己的 touchesBegan touchesMoved 等方法。

Instead of adding a gesture recognizer I am subclassing the UIScrollView and writing my own touchesBegan, touchesMoved, etc methods.

这样我知道用户在哪里触摸但不幸的是PanGestureRecognizer正在触发 touchesCancelled 每次我开始滚动,即使将 canCancelContentTouches 设置为NO

This way I know where the user is touching BUT unfortunately the PanGestureRecognizer is triggering touchesCancelled every time I start to scroll even after setting the canCancelContentTouches to NO.

有人知道为什么吗?我还找到了。

Does anybody know why? I have also found this.

这篇关于在UIScrollView中向标准Pan Gesture Recognizer添加功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:30