当你在使用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;
}
}