您的位置:首页 > 移动开发 > IOS开发

iOS 屏幕左侧向右滑动返回

2017-04-25 09:27 555 查看



首先,你的Vc是有NavigationController来管理的,就是得有导航栏.

这个人性化的便捷操作从iOS7开始.


为什么有些ViewController可以左侧滑动返回,有些页面这个手势就不起作用了?

手势可用:当前页面的返回(pop)事件没有被自定义,使用的是系统的back,interactivePopGestureRecognizer.enable =YES ,默认是开启的状态,看名字也知道Ta干什么的.

手势失效: 当自定义了返回的图标样式,文案等信息后,手势的值就变了 → → →interactivePopGestureRecognizer.enable = NO


针对上面的(2)问题,来看看怎么在次开启这个功能

这个功能,就是NavigationController中的interactivePopGestureRecognizer属性,原理在上面已经介绍的差不多了,只是一个属性.

手动开启要注意这个点 

1. 如果手动开启了interactivePopGestureRecognizer,在根Vc中要把Ta设置成NO,不然滑动依旧生效,但是已经没有上级Vc,找不到就会导致程序Crash.


手动开启的思路

开启功能:要在根VC的viewWillDisAppear方法中设置当前的NavigationController.的interactivePopGestureRecognizer.enable =YES;

根VC的viewDidAppear等方法中设置interactivePopGestureRecognizer.enable =NO;

开启代码如下:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
1
2
3
4
5
6
7

关于上文提到的注意点
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//禁用返回手势
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
1
2
3
4
5
6
7
8

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: