在 UIScrollView 上拦截平移手势会中断滚动

Intercepting pan gestures over a UIScrollView breaks scrolling(在 UIScrollView 上拦截平移手势会中断滚动)

本文介绍了在 UIScrollView 上拦截平移手势会中断滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个垂直滚动的 UIScrollView.我还想在其上处理水平平移,同时允许默认的垂直滚动行为.我在滚动视图上放置了一个透明的 UIView,并添加了一个平移手势识别器.这样我可以很好地得到平底锅,但是滚动视图没有收到任何手势.

I have a vertically-scrolling UIScrollView. I want to also handle horizontal pans on it, while allowing the default vertical scroll behavior. I've put a transparent UIView over the scroll view, and added a pan gesture recognizer to it. This way I can get the pans just fine, but then the scroll view doesn't receive any gestures.

我已经实现了以下 UIPanGestureRecognizerDelegate 方法,希望将我的手势识别器限制为仅水平平移,但这并没有帮助:

I've implemented the following UIPanGestureRecognizerDelegate methods, hoping to limit my gesture recognizer to horizontal pans only, but that didn't help:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    // Only accept horizontal pans here.
    // Leave the vertical pans for scrolling the content.
    CGPoint translation = [gestureRecognizer translationInView:self.view];
    BOOL isHorizontalPan = (fabsf(translation.x) > fabsf(translation.y));
    return  isHorizontalPan;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return (otherGestureRecognizer == _scrollView.panGestureRecognizer);
}

推荐答案

好的,我想通了.我需要做两件事来完成这项工作:

OK, I figured it out. I needed to do 2 things to make this work:

1) 将我自己的平移识别器附加到滚动视图本身,而不是附加到它上面的另一个视图.

1) Attach my own pan recognizer to the scroll view itself, not to another view on top of it.

2) 此 UIGestureRecognizerDelegate 方法可防止同时调用默认滚动视图和我自己的滚动视图时发生的愚蠢行为.

2) This UIGestureRecognizerDelegate method prevents the goofy behavior that happens when both the default scrollview and my own one are invoked simultaneously.

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

这篇关于在 UIScrollView 上拦截平移手势会中断滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 UIScrollView 上拦截平移手势会中断滚动

基础教程推荐