ios7开始 苹果增加了页面 右滑返回的效果;具体的是以UINavigationController为容器的ViewController间右滑切换页面。
代码里的设置是:
self.navigationController.interactivePopGestureRec可以看到苹果给navigationController添加了一个手势(具体为UIScreenEdgePanGestureReognizer.enabled = YES;(default is YES)
问题1:
然而事情并非我们想的那么简单。
1.当我们用系统的UINavigationController,并且也是利用系统的navigateBar的时候,是完全没有问题的
2.但是当我们没有用系统的navigateBar或者自定义了返回按钮的时候,这个时候 右滑返回是失效的。
解决(问题1)办法:
对于这种失效的情况,考虑到 interactivePopGestureRec
我们可以在主 NavigationController中设置一下:
self.navigationController.interactivePopGestureRec
问题2:
但是出现很多问题,比如说在rootViewController的时候这个手势也可以响应,导致整个程序页面不响应;push了多层后,快速的触发两次手势,也会错乱
解决(问题2)办法:
借鉴了别人的方法:具体是通过 获取当前pushView栈里的当前显示的VC,根据这个VC来决定 是否开启手势(如果currentShowVC 是当前显示的,则开启手势;如果 currentShowVC为nil,则代表在主页面,关闭手势)
注:
当时试了一种方法 就是滑动的时候来回设置 interactivePopGestureRec
至此,觉得ios7上的右滑返回大功告成了,心里正happy,妈蛋,发现了一个可耻的bug:
UIScrollView 上 右滑返回的手势失灵了,靠!!!!!!
问题三:
UIScrollView上手势失灵:
经研究,发现是 UIScrollView上已经添加了 panGestureRecognizer(滑动)手势
解决(问题三)办法:
参考:http://www.cnblogs.com/lexingyu/p/3702742.html
【解决方案】
苹果以UIGestureRecognizerDeleg
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 shouldRecognizeSimultaneouslyWithGestureRecogniz er:(UIGestureRecognizer *)otherGestureRecognizer;
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultane
{
}
事实上,对UIGestureRecognizer来说,它们对事件的接收顺序和对事件的响应是可以分开设置的,即存在接收链和响应链。接收链如上文所述,和UIView绑定,由UIView的层次决定接收顺序。
而响应链在apple君的定义下,逻辑出奇的简单,只有一个方法可以设置多个gestureRecognizer的响应关系:
// create a relationship with another gesture recognizer that will prevent this gesture's actions from being called until otherGestureRecognizer transitions to UIGestureRecognizerStateFailed // if otherGestureRecognizer transitions to UIGestureRecognizerState Recognized or UIGestureRecognizerState Began then this recognizer will instead transition to UIGestureRecognizerState Failed // example usage: a single tap may require a double tap to fail - (void)requireGestureRecognizer ToFail:(UIGestureRecognizer *)otherGestureRecognizer;
每个UIGesturerecognizer都是一个有限状态机,上述方法会在两个gestureRecognizer间建立一个依托于state的依赖关系,当被依赖的gestureRecognizer.state = failed时,另一个gestureRecognizer才能对手势进行响应。
所以,只需要
[_scrollView.panGestureRecognizer requireGestureRecognizerToFail:screenEdgePanGestureReco gnizer];
{
}
参考:
牛逼 解决了iOS7下滑动返回与ScrollView共存
http://www.cnblogs.com/lexingyu/p/3702742.html
更牛逼
http://www.2cto.com/kf/201401/272886.html