在 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 上拦截平移手势会中断滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
Handling scroll views with (custom, interactive) view controller presentation and dismissal(使用(自定义、交互式)视图控制器呈现和解除处理滚动视图)