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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13736399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:26:33  来源:igfitidea点击:

Intercepting pan gestures over a UIScrollView breaks scrolling

iosios5uiscrollviewuigesturerecognizeruipangesturerecognizer

提问by Macondo2Seattle

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 UIViewover 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.

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

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

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

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

回答by Macondo2Seattle

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

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

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

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

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

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

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

回答by tony.tc.leung

I had the same problem to solve and I did this:

我有同样的问题要解决,我这样做了:

1) Attach my own pan recognizer to the scroll view.

1) 将我自己的平移识别器附加到滚动视图。

2) Return YES on: – gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

2) 返回YES: –gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

This will allow both gestures to work. So what that means is that on vertical scroll, both your panGesture delegate and scrollView Delegate will be fired. If it is a horizontal scroll, it will only call your panGesture delegate.

这将允许两种手势工作。所以这意味着在垂直滚动时,您的 panGesture 委托和 scrollView 委托都将被触发。如果是水平滚动,它只会调用您的 panGesture 委托。

3) in my panGesture delegate, detect if it is a horizontal scroll, if it is not, ignore.

3) 在我的 panGesture 委托中,检测它是否是水平滚动,如果不是,则忽略。

回答by bnussey

Swift answer:

迅捷回答:

let scrollViewPanGesture = UIPanGestureRecognizer(target: self, action: #selector(onPan(_:)))
scrollViewPanGesture.delegate = self
scrollView.addGestureRecognizer(scrollViewPanGesture)

extension ViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}