Intercepting pan gestures over a UIScrollView breaks scrolling(在 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 上拦截平移手势会中断滚动
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01