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 上拦截平移手势会中断滚动
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
