【转载请注明出处】
iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势。支持此手势的是UINavigationController中新增的属性
interactivePopGestureRecognizer,即右滑返回只支持以UINavigationController为容器的ViewController间切换,要想在自定义容器中使用,需要一些额外的工作。
基本地,控制ViewController是否启用右滑返回,只需要这样:
<span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span> <span class="keyword" style="font-weight: bold;">self</span>.navigationController.interactivePopGestureRecognizer.enabled = <span class="constant">YES</span>;
默认情况下enabled为YES。
在实际使用中,遇到了一些问题,整理如下:
1、自定义返回按钮后,右滑返回失效;
解决方案:比较直观的办法是在自定义返回按钮时,使用backBarButtonItem:
<span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span> UIButton *backButton =<span style="color: rgb(0, 0, 0);"> [UIButton buttonWithType:UIButtonTypeCustom];
</span><span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">2</span></span> <span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">some initialize code here...</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">3</span></span> UIBarButtonItem *barItem =<span style="color: rgb(0, 0, 0);"> [[UIBarButtonItem alloc] initWithCustomView:backButton];
</span><span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">4</span></span> <span style="text-decoration: line-through;"><span class="keyword" style="font-weight: bold;">self</span>.navigationItem.leftBarButtonItem = barItem;</span> <span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">not working</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">5</span></span> <span class="keyword" style="font-weight: bold;">self</span>.navigationItem.backBarButtonItem = barItem; <span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">serve well</span></span><span style="color: rgb(0, 128, 0);"></span>
P.S:关于backBarButtonItem和leftBarButtonItem的区别:
http://www.cocoachina.com/ask/questions/show/97110
但这样无法支持左上角多个按钮的情况。考虑到 interactivePopGestureRecognizer也有delegate属性, 替换默认的 self . navigationController .interactivePopGestureRecognizer.delegate来配置右滑返回的表现也是可行的。在主ViewController中:
<span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span> <span class="keyword" style="font-weight: bold;">self</span>.navigationController.interactivePopGestureRecognizer.<span style="color: rgb(0, 0, 255);">delegate</span> = <span class="keyword" style="font-weight: bold;">self</span>;
<span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">1</span></span> - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *<span style="color: rgb(0, 0, 0);">)gestureRecognizer
</span><span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">2</span></span> <span style="color: rgb(0, 0, 0);">{
</span><span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">3</span></span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">if</span></span> (<span class="keyword" style="font-weight: bold;">self</span>.navigationController.viewControllers.count == <span class="number" style="color: rgb(0, 153, 153);">1</span>)<span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);">关闭主界面的右滑返回</span></span><span style="color: rgb(0, 128, 0);"></span>
<span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">4</span></span> <span style="color: rgb(0, 0, 0);"> {
</span><span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">5</span></span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">return</span></span><span style="color: rgb(0, 0, 0);"> NO;
</span><span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">6</span></span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">7</span></span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">else</span></span>
<span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">8</span></span> <span style="color: rgb(0, 0, 0);"> {
</span><span style="color: rgb(0, 128, 128);"> <span class="number" style="color: rgb(0, 153, 153);">9</span></span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">return</span></span><span style="color: rgb(0, 0, 0);"> YES;
</span><span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">10</span></span> <span style="color: rgb(0, 0, 0);"> }
</span><span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">11</span></span> }
如此做的好处是可以在主ViewController中配置栈中所有ViewController右滑返回的开启,而不需要在各个ViewController中分别设置enabled。
值得注意的是:在替换了delegate之后,必须在gestureRecognizerShouldBegin:中设置某ViewController A开启右滑返回,同时在A中未设置interactivePopGestureRecognizer.enabled = NO,右滑返回才会开启,即二者中任一为NO,右滑返回都处于关闭状态。
2、主界面(UINavigationController栈中的第一个ViewController)默认也是开启右滑返回的。若在主界面上右滑,不会有动作执行。但此时想进入下一级ViewController(如点击tableView中某一行),切换动画却没有出现。切回桌面再进入应用,发现直接进入了下一级ViewController。
解决方案:这个问题是在最初试验右滑返回的使用方式时出现的。在使用自定义返回按钮的ViewController中
<span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span> <span class="keyword" style="font-weight: bold;">self</span>.navigationController.interactivePopGestureRecognizer.<span style="color: rgb(0, 0, 255);">delegate</span> = <span class="keyword" style="font-weight: bold;">self</span>;
解决解决问题1的同时,造成了问题2。和1中相似,都是在替换了默认的delegate之后,interactivePopGestureRecognizer就能调用自定义的返回方法了。具体原因尚不清楚,待更新【Mark】。
3、在使用右滑返回拖动到一半时,有时会在导航栏上看到三个排成一行的小蓝点。
解决方案:原因不明,解决方案不明。
P.S:在一个帖子上看到一个办法:
<span style="color: rgb(0, 128, 128);"><span class="number" style="color: rgb(0, 153, 153);">1</span></span> self.navigationItem.title = <span style="color: rgb(128, 0, 0);"><span class="string" style="color: rgb(221, 17, 68);">@""</span></span>;
可以隐藏小蓝点,但由于小蓝点非必现,在不明究竟的情况下很难说是否有效。
帖子链接: http://www.tuicool.com/articles/FB3IJ3
(1)在工程中查看, self . navigationController .interactivePopGestureRecognizer.delegate实际上是一个
_UINavigationInteractiveTransition实例,该类声明如下:
1 <span class="at_rule">@class</span><span class="at_rule"> UIScreenEdgePanGestureRecognizer</span>;
2
3 <span class="at_rule">@interface</span><span class="at_rule"> _UINavigationInteractiveTransition : _UINavigationInteractiveTransitionBase </span>{
4 <span class="tag" style="color: rgb(0, 0, 128);">UIScreenEdgePanGestureRecognizer</span> *_<span class="tag" style="color: rgb(0, 0, 128);">edgePanRecognizer</span>;
5 }
6
7 <span class="at_rule">@<span class="function">property(readonly)</span> UIScreenEdgePanGestureRecognizer * screenEdgePanGestureRecognizer</span>;
8
9 - (<span class="tag" style="color: rgb(0, 0, 128);">void</span>)_<span class="tag" style="color: rgb(0, 0, 128);">configureNavigationGesture</span>;
10 - (<span class="tag" style="color: rgb(0, 0, 128);">BOOL</span>)_<span class="tag" style="color: rgb(0, 0, 128);">gestureRecognizer</span><span class="pseudo">:(id)arg1</span> <span class="tag" style="color: rgb(0, 0, 128);">shouldBeRequiredToFailByGestureRecognizer</span><span class="pseudo">:(id)arg2</span>;
11 - (<span class="tag" style="color: rgb(0, 0, 128);">void</span>)<span class="tag" style="color: rgb(0, 0, 128);">dealloc</span>;
12 - (<span class="tag" style="color: rgb(0, 0, 128);">BOOL</span>)<span class="tag" style="color: rgb(0, 0, 128);">gestureRecognizer</span><span class="pseudo">:(id)arg1</span> <span class="tag" style="color: rgb(0, 0, 128);">shouldReceiveTouch</span><span class="pseudo">:(id)arg2</span>;
13 - (<span class="tag" style="color: rgb(0, 0, 128);">BOOL</span>)<span class="tag" style="color: rgb(0, 0, 128);">gestureRecognizer</span><span class="pseudo">:(id)arg1</span> <span class="tag" style="color: rgb(0, 0, 128);">shouldRecognizeSimultaneouslyWithGestureRecognizer</span><span class="pseudo">:(id)arg2</span>;
14 - (<span class="tag" style="color: rgb(0, 0, 128);">BOOL</span>)<span class="tag" style="color: rgb(0, 0, 128);">gestureRecognizerShouldBegin</span><span class="pseudo">:(id)arg1</span>;
15 - (<span class="tag" style="color: rgb(0, 0, 128);">id</span>)<span class="tag" style="color: rgb(0, 0, 128);">gestureRecognizerView</span>;
16 - (<span class="tag" style="color: rgb(0, 0, 128);">id</span>)<span class="tag" style="color: rgb(0, 0, 128);">initWithViewController</span><span class="pseudo">:(id)arg1</span> <span class="tag" style="color: rgb(0, 0, 128);">animator</span><span class="pseudo">:(id)arg2</span>;
17 - (<span class="tag" style="color: rgb(0, 0, 128);">id</span>)<span class="tag" style="color: rgb(0, 0, 128);">screenEdgePanGestureRecognizer</span>;
18 - (<span class="tag" style="color: rgb(0, 0, 128);">void</span>)<span class="tag" style="color: rgb(0, 0, 128);">setNotInteractiveTransition</span>;
19 - (<span class="tag" style="color: rgb(0, 0, 128);">void</span>)<span class="tag" style="color: rgb(0, 0, 128);">startInteractiveTransition</span>;
20
21 <span class="at_rule">@end</span>
可以看到,委托的内部,实际上是一个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】。
1、UIScreenEdgePanGestureRecognizer Class Reference
2、_UINavigationInteractiveTransition.h
3、自定义返回按钮时,iOS7手势返回遇到的问题