美文网首页iOS开发技巧iOS学习iOS菜鸟级开发
iOS 下如果存在UIScrollerView 使用UIScre

iOS 下如果存在UIScrollerView 使用UIScre

作者: Z_Lukas | 来源:发表于2015-12-28 19:01 被阅读3243次

当你在使用UIScreenEdgePanGestureRecognizer手势实现侧滑的时候,如果后期你导航控制器push出的界面中包含UIScrollerView,这个时候你会发现,侧滑效果无法实现了,这个首先你会想到肯定是UIScrollerView,把这个手势给拦截了,执行了UIScrollerView中包含的手势。

问题所在

滑动返回事实上也是由于存在已久的UIScreenEdgePanGestureRecognizer来识别并且相应地,它直接与UINavigationController的view进行了绑定,绑定的方法是写在UINavgationController 的基类中的,正如一下:

UIPanGestureRecongnizer -- bind-- UIScrollerView
UIScreenEdgePanGestureRecognizer --bind-- UINavigationController.view

滑动返回无法触发,说明UIScreenEdgePanGestureRecongnizer并没有接受到手势事件。
根据苹果的<a href= "https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html#//apple_ref/doc/uid/TP40009541-CH2-SW2">官方文档</a>说明 UIGestureRecongnizer 和UIview 是多对一的关系,UIGestureRecognizer 一定要和UIView进行绑定才能发挥作用,因此UIGestureRecongnizer对于屏幕上的手势事件,其接受顺序和UIView的层次结构是一致的,如下关系

UINavgataionController.view -->UIviewController.view -- > UIScrollerView.view -->screen and user'finger 既UIScrollView的panGestureRecognizer
先接受到了手势事件,直接就处理而没有往下传递实际上就是两个手势共存的问题

解决方案

UIGestureRecognizerDelegate 代理方法中包含,支持多个UIGestureRecongnizer共存,其中一个方法是

1 // called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other
2 // return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)
3 // 
4 // note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES
5 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

总结就是此方法返回YES,手势事件会一直往下传递,不论当前层次是否对该事件进行响应

看看UIScrollerView的头文件的描述:

1 // Use these accessors to configure the scroll view's built-in gesture recognizers.
2 // Do not change the gestures' delegates or override the getters for these properties.
3 @property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer NS_AVAILABLE_IOS(5_0);

UIScrollView本身是其panGestureRecognizer的delegate,且apple君明确表明不能修改它的delegate(修改的时候也会有警告)

UIScrollView作为delegate,说明UIScrollView中实现了上文提到的shouldRecognizeSimultaneouslyWithGestureRecognizer方法,返回了NO。创建一个UIScrollView的category,由于category中的同名方法会覆盖原有.m文件中的实现,使得可以自定义手势事件的传递,如下:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ 
      if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]  && [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) { 
            return YES; 
      } else  {
            return NO; 
     }
 }

相关文章

网友评论

  • b30b9a57a446:继承scrollview重写 hittest 方法就可以了
  • 大号鱼骨头: :blush: 手势冲突很蛋疼,还是需要了解一下原理,才能更有效率地解决。
  • 为之则易ing:没这么麻烦,scrollView的bounces设为NO就OK了
    为之则易ing:@大猿媛 可以啊,当时是这么解决的
    大猿媛:基类UINavigationController里实现了UIScreenPanGesture的相关方法, 在一个controller里,有一个scrollview,发现左滑动返回失效,你确定bounces设为NO就行了?我的不可以哦

本文标题:iOS 下如果存在UIScrollerView 使用UIScre

本文链接:https://www.haomeiwen.com/subject/eoobhttx.html