【转载请注明出处】php
iOS 7中在传统的左上角返回键以外,提供了右滑返回上一级界面的手势。支持此手势的是UINavigationController中新增的属性css
interactivePopGestureRecognizer,即右滑返回只支持以UINavigationController为容器的ViewController间切换,要想在自定义容器中使用,须要一些额外的工做。html
基本地,控制ViewController是否启用右滑返回,只须要这样:ios
.navigationController.interactivePopGestureRecognizer.enabled = ;
默认状况下enabled为YES。git
在实际使用中,遇到了一些问题,整理以下:
一、自定义返回按钮后,右滑返回失效;github
解决方案:比较直观的办法是在自定义返回按钮时,使用backBarButtonItem:ruby
UIButton *backButton = UIBarButtonItem *barItem = .navigationItem.backBarButtonItem = barItem;
P.S:关于backBarButtonItem和leftBarButtonItem的区别:app
http://www.cocoachina.com/ask/questions/show/97110动画
但这样没法支持左上角多个按钮的状况。考虑到 interactivePopGestureRecognizer也有delegate属性, 替换默认的 self . navigationController .interactivePopGestureRecognizer.delegate来配置右滑返回的表现也是可行的。在主ViewController中:ui
.navigationController.interactivePopGestureRecognizer. = ;
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer * (.navigationController.viewControllers.count == ) }
如此作的好处是能够在主ViewController中配置栈中全部ViewController右滑返回的开启,而不须要在各个ViewController中分别设置enabled。
值得注意的是:在替换了delegate以后,必须在gestureRecognizerShouldBegin:中设置某ViewController A开启右滑返回,同时在A中未设置interactivePopGestureRecognizer.enabled = NO,右滑返回才会开启,即两者中任一为NO,右滑返回都处于关闭状态。
二、主界面(UINavigationController栈中的第一个ViewController)默认也是开启右滑返回的。若在主界面上右滑,不会有动做执行。但此时想进入下一级ViewController(如点击tableView中某一行),切换动画却没有出现。切回桌面再进入应用,发现直接进入了下一级ViewController。
解决方案:这个问题是在最初试验右滑返回的使用方式时出现的。在使用自定义返回按钮的ViewController中
.navigationController.interactivePopGestureRecognizer. = ;
解决解决问题1的同时,形成了问题2。和1中类似,都是在替换了默认的delegate以后,interactivePopGestureRecognizer就能调用自定义的返回方法了。具体缘由尚不清楚,待更新【Mark】。
三、在使用右滑返回拖动到一半时,有时会在导航栏上看到三个排成一行的小蓝点。
解决方案:缘由不明,解决方案不明。
P.S:在一个帖子上看到一个办法:
self.navigationItem.title = ;
能够隐藏小蓝点,但因为小蓝点非必现,在不明究竟的状况下很难说是否有效。
帖子连接: http://www.tuicool.com/articles/FB3IJ3
(1)在工程中查看, self . navigationController .interactivePopGestureRecognizer.delegate其实是一个
_UINavigationInteractiveTransition实例,该类声明以下:
1 ; 2 3 { 4 *_; 5 } 6 7 ; 8 9 - ()_; 10 - ()_ ; 11 - (); 12 - () ; 13 - () ; 14 - (); 15 - (); 16 - () ; 17 - (); 18 - (); 19 - (); 20 21
能够看到,委托的内部,其实是一个UIScreenEdgePanGestureRecognizer实例在起做用,它是iOS7中引入的一个新类,用于支持某些状况下ViewController间切换的初始化。apple官方文档中对其的描述不多,以下:
A UIScreenEdgePanGestureRecognizer
looks for panning (dragging) gestures that start near an edge of the screen. The system uses screen edge gestures in some cases to initiate view controller transitions. You can use this class to replicate the same gesture behavior for your own actions.
After creating a screen edge pan gesture recognizer, assign an appropriate value to the edges
property before attaching the gesture recognizer to your view. You use this property to specify from which edges the gesture may start. This gesture recognizer ignores any touches beyond the first touch.
要在自定义的ViewController容器中支持右滑返回,可能就须要用到它。
(2)目前很多应用仍是用的iOS 6.1 SDK,而许多iOS7的用户对右滑返回的需求很是迫切,所以在iOS 6.1SDK下模拟右滑返回在短期内是有必要的,如下是一个经过在push时截取上级ViewController界面为UIImage做为下一级ViewController的背景的一种实现方式:
做者的本意彷佛并不要要模拟右滑返回,但稍做修改就能在结构比较简单的应用中使用,如下是连接:
https://github.com/vinqon/MultiLayerNavigation
P.S:对于一些特殊的需求,如在有ScrollView的界面上(好比浏览照片)模拟右滑返回,当滑动到最左边时即执行右滑返回,该类没法知足,待处理【Mark】。
一、UIScreenEdgePanGestureRecognizer Class Reference
二、_UINavigationInteractiveTransition.h
三、自定义返回按钮时,iOS7手势返回遇到的问题