#import "STKNavigationController.h"
@interface STKNavigationController ()<UINavigationControllerDelegate>
@property(nonatomic,weak) UIViewController* currentShowVC;
@end
@implementation STKNavigationController
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController{
if (self = [super initWithRootViewController:rootViewController]) {
STKNavigationController *nav = [super initWithRootViewController:rootViewController];
nav.interactivePopGestureRecognizer.enabled = YES;
nav.interactivePopGestureRecognizer.delegate = self;
nav.delegate = self;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// __weak typeof(self) weakSelf = self;
// if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
// self.interactivePopGestureRecognizer.delegate = weakSelf;
// }
}
#pragma mark UINavigationControllerDelegate
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
}
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (navigationController.viewControllers.count == 1)
self.currentShowVC = nil;
else
self.currentShowVC = viewController;
}
#pragma mark UIGestureRecognizerDelegate
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
return (self.currentShowVC == self.topViewController); //the most important
}
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
return YES;
} else {
return NO;
}
}
前言:
ios7开始 苹果增加了页面 右滑返回的效果;具体的是以UINavigationController为容器的ViewController间右滑切换页面。
代码里的设置是:
self.navigationController.interactivePopGestureRecognizer.enabled = YES;(default is YES)
可以看到苹果给navigationController添加了一个手势(具体为UIScreenEdgePanGestureRecognizer(边缘手势,同样是ios7以后才有的)),就是利用这个手势实现的 ios7的侧滑返回。
问题1:
然而事情并非我们想的那么简单。
1.当我们用系统的UINavigationController,并且也是利用系统的navigateBar的时候,是完全没有问题的
2.但是当我们没有用系统的navigateBar或者自定义了返回按钮的时候,这个时候 右滑返回是失效的。
解决(问题1)办法:
对于这种失效的情况,考虑到interactivePopGestureRecognizer也有delegate属性,替换默认的self.navigationController.interactivePopGestureRecognizer.delegate来配置右滑返回的表现也是可行的。
我们可以在主NavigationController中设置一下:
self.navigationController.interactivePopGestureRecognizer.delegate =(id)self
问题2:
但是出现很多问题,比如说在rootViewController的时候这个手势也可以响应,导致整个程序页面不响应;push了多层后,快速的触发两次手势,也会错乱
解决(问题2)办法:
@interface NavRootViewController : UINavigationController
@property(nonatomic,weak) UIViewController* currentShowVC;
@end
@implementation NavRootViewController
-(id)initWithRootViewController:(UIViewController )rootViewController
{
NavRootViewController nvc = [super initWithRootViewController:rootViewController];
self.interactivePopGestureRecognizer.delegate = self;
nvc.delegate = self;
return nvc;
}
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
}
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (navigationController.viewControllers.count == 1)
self.currentShowVC = Nil;
else
self.currentShowVC = viewController;
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.interactivePopGestureRecognizer) {
return (self.currentShowVC == self.topViewController); //the most important
}
return YES;
}
@end
借鉴了别人的方法:具体是通过 获取当前pushView栈里的当前显示的VC,根据这个VC来决定 是否开启手势(如果currentShowVC 是当前显示的,则开启手势;如果 currentShowVC为nil,则代表在主页面,关闭手势)
注:
当时试了一种方法 就是滑动的时候来回设置 interactivePopGestureRecognizer的delegate;发现 会有crash,原因就是 因为 我把 当前显示的VC设置为了这个手势的delegate,但当这个VC消失的时候,这个delegate便被释放了,导致crash
至此,觉得ios7上的右滑返回大功告成了,心里正happy,妈蛋,发现了一个可耻的bug:
UIScrollView 上 右滑返回的手势失灵了,靠!!!!!!
问题三:
UIScrollView上手势失灵:
经研究,发现是UIScrollView上已经添加了 panGestureRecognizer(滑动)手势
ios7 <wbr>侧滑返回
解决(问题三)办法:
参考:http://www.cnblogs.com/lexingyu/p/3702742.html
【解决方案】
苹果以UIGestureRecognizerDelegate的形式,支持多个UIGestureRecognizer共存。其中的一个方法是:
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时,手势事件会一直往下传递,不论当前层次是否对该事件进行响应。
@implementation UIScrollView (AllowPanGestureEventPass)(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]
&& [otherGestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]])
{
return YES;
}
else
{
return NO;
}
}
事实上,对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 UIGestureRecognizerStateRecognized or UIGestureRecognizerStateBegan then this recognizer will instead transition to UIGestureRecognizerStateFailed // example usage: a single tap may require a double tap to fail - (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
每个UIGesturerecognizer都是一个有限状态机,上述方法会在两个gestureRecognizer间建立一个依托于state的依赖关系,当被依赖的gestureRecognizer.state = failed时,另一个gestureRecognizer才能对手势进行响应。
所以,只需要
[_scrollView.panGestureRecognizer requireGestureRecognizerToFail:screenEdgePanGestureRecognizer];(UIScreenEdgePanGestureRecognizer *)screenEdgePanGestureRecognizer
{
UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer = nil;
if (self.view.gestureRecognizers.count > 0)
{
for (UIGestureRecognizer *recognizer in self.view.gestureRecognizers)
{
if ([recognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]])
{
screenEdgePanGestureRecognizer = (UIScreenEdgePanGestureRecognizer *)recognizer;
break;
}
}
}
return screenEdgePanGestureRecognizer;
}
参考:
牛逼 解决了iOS7下滑动返回与ScrollView共存
http://www.cnblogs.com/lexingyu/p/3702742.html
更牛逼 解决了interactivePopGestureRecognizer.delegate 的 释放导致crash的问题
http://www.2cto.com/kf/201401/272886.htm
\\\\\\\\\\\\\\\\\\\\\\\\\
iOS 7中在传统的左上角返回键之外,提供了右滑返回上一级界面的手势。支持此手势的是UINavigationController中新增的属性
interactivePopGestureRecognizer,即右滑返回只支持以UINavigationController为容器的ViewController间切换,要想在自定义容器中使用,需要一些额外的工作。
基本地,控制ViewController是否启用右滑返回,只需要这样:
1 self.navigationController.interactivePopGestureRecognizer.enabled = YES;
默认情况下enabled为YES。
在实际使用中,遇到了一些问题,整理如下:
1、自定义返回按钮后,右滑返回失效;
解决方案:比较直观的办法是在自定义返回按钮时,使用backBarButtonItem:
1 UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
2 //some initialize code here...
3 UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
4 self.navigationItem.leftBarButtonItem = barItem; //not working
5 self.navigationItem.backBarButtonItem = barItem; //serve well
P.S:关于backBarButtonItem和leftBarButtonItem的区别:
http://www.cocoachina.com/ask/questions/show/97110
但这样无法支持左上角多个按钮的情况。考虑到 interactivePopGestureRecognizer也有delegate属性, 替换默认的 self . navigationController .interactivePopGestureRecognizer.delegate来配置右滑返回的表现也是可行的。在主ViewController中:
1 self.navigationController.interactivePopGestureRecognizer.delegate = self;
1 - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
2 {
3 if (self.navigationController.viewControllers.count == 1)//关闭主界面的右滑返回
4 {
5 return NO;
6 }
7 else
8 {
9 return YES;
10 }
11 }
如此做的好处是可以在主ViewController中配置栈中所有ViewController右滑返回的开启,而不需要在各个ViewController中分别设置enabled。
值得注意的是:在替换了delegate之后,必须在gestureRecognizerShouldBegin:中设置某ViewController A开启右滑返回,同时在A中未设置interactivePopGestureRecognizer.enabled = NO,右滑返回才会开启,即二者中任一为NO,右滑返回都处于关闭状态。
2、主界面(UINavigationController栈中的第一个ViewController)默认也是开启右滑返回的。若在主界面上右滑,不会有动作执行。但此时想进入下一级ViewController(如点击tableView中某一行),切换动画却没有出现。切回桌面再进入应用,发现直接进入了下一级ViewController。
解决方案:这个问题是在最初试验右滑返回的使用方式时出现的。在使用自定义返回按钮的ViewController中
1 self.navigationController.interactivePopGestureRecognizer.delegate = self;
解决解决问题1的同时,造成了问题2。和1中相似,都是在替换了默认的delegate之后,interactivePopGestureRecognizer就能调用自定义的返回方法了。具体原因尚不清楚,待更新【Mark】。
3、在使用右滑返回拖动到一半时,有时会在导航栏上看到三个排成一行的小蓝点。
解决方案:原因不明,解决方案不明。
P.S:在一个帖子上看到一个办法:
1 self.navigationItem.title = @"";
可以隐藏小蓝点,但由于小蓝点非必现,在不明究竟的情况下很难说是否有效。
帖子链接: http://www.tuicool.com/articles/FB3IJ3
(1)在工程中查看, self . navigationController .interactivePopGestureRecognizer.delegate实际上是一个
_UINavigationInteractiveTransition实例,该类声明如下:
1 @class UIScreenEdgePanGestureRecognizer;
2
3 @interface _UINavigationInteractiveTransition : _UINavigationInteractiveTransitionBase {
4 UIScreenEdgePanGestureRecognizer *_edgePanRecognizer;
5 }
6
7 @property(readonly) UIScreenEdgePanGestureRecognizer * screenEdgePanGestureRecognizer;
8
9 - (void)_configureNavigationGesture;
10 - (BOOL)_gestureRecognizer:(id)arg1 shouldBeRequiredToFailByGestureRecognizer:(id)arg2;
11 - (void)dealloc;
12 - (BOOL)gestureRecognizer:(id)arg1 shouldReceiveTouch:(id)arg2;
13 - (BOOL)gestureRecognizer:(id)arg1 shouldRecognizeSimultaneouslyWithGestureRecognizer:(id)arg2;
14 - (BOOL)gestureRecognizerShouldBegin:(id)arg1;
15 - (id)gestureRecognizerView;
16 - (id)initWithViewController:(id)arg1 animator:(id)arg2;
17 - (id)screenEdgePanGestureRecognizer;
18 - (void)setNotInteractiveTransition;
19 - (void)startInteractiveTransition;
20
21 @end
可以看到,委托的内部,实际上是一个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手势返回遇到的问题
http://www.tuicool.com/articles/FB3IJ3
http://www.tuicool.com/articles/vMfAVv
4、饼状图