在UIScrollView中向标准Pan Gesture Recognizer添加function

我试图跟踪手指在UIScrollView 。 我已经将UIScrollView子类化(见下文),但遗憾的是我添加的手势识别器正在覆盖标准的手势识别器。

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

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

谢谢。

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

如果你想要它不要覆盖标准的那个,你只需要同时识别它们。

 - (void)viewDidLoad:(BOOL)animated { [super viewDidLoad:animated]; UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; panRecognizer.delegate = self; [scrollView addGestureRecognizer:panRecognizer]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return TRUE; } - (void)pan:(id)sender { NSLog(@"Pan"); } 

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

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

我没有添加手势识别器,而是touchesBegan UIScrollView并编写自己的touchesBegantouchesMoved等方法。

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

有人知道为什么吗? 我也发现了这个 。