iOS左滑手势失效的解决方法

2025-05-29 0 36

iOS7之后,苹果优化了一个小功能,就是对于UINavagationController堆栈里的UIViewController,只要轻轻在视图控制器的左边缘右滑一下,该视图控制器就会pop出栈(前提当然是对于非根视图控制器而言)。实现方法很简单,一句话搞定:

?

1
self.navigationController.interactivePopGestureRecognizer.enabled = YES;

事实上对于一个视图控制器而言,该属性的默认值即为YES,因此不设置也能实现右滑pop的功能。

然而这个功能很有局限性,因为它不允许当前视图控制器自定义了leftBarButtonItem,一旦自定义,右滑功能就会失效。这里有一个方法:

?

1
self.navigationController.interactivePopGestureRecognizer.delegate = nil;

设置代理为nil之后即便自定义了leftBarButtonItem也可以右滑pop。

或者,把手势的许可打开 也可:

?

1
self.navigationController.interactivePopGestureRecognizer.enabled = YES ;

事实上如果自定义了leftBarButtonItem,常用的做法是重新设置代理:

?

1

2

3
- (void)viewDidAppear:(BOOL)animated{

self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

}

然后实现手势协议即可:

?

1

2

3

4

5

6

7

8
#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer{

//判断是否为rootViewController

if (self.navigationController && self.navigationController.viewControllers.count == 1) {

return NO;

}

return YES;

}

不过呢,如果我们自定义的返回button只是文字或图片的话,这样设置就可以,不会失效

?

1

2

3
UIBarButtonItem *item = [[UIBarButtonItem alloc]init];

item.title = @"";

self.navigationItem.backBarButtonItem = item;

如果是要自定义view的当作button的话,就要用leftBarButtonItem设置,并用上述讲的防止手势失效的方案.

有朋友提出以上方式在多次滑动之后会导致界面假死,这里再给出一种解决方案:

在所有除一级页面之外的页面的viewDidAppear和viewWillDisappear中加入以下代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

//代理置空,否则会闪退

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

self.navigationController.interactivePopGestureRecognizer.delegate = nil;

}

}

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

//开启iOS7的滑动返回效果

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

//只有在二级页面生效

if ([self.navigationController.viewControllers count] == 2) {

self.navigationController.interactivePopGestureRecognizer.delegate = self;

}

}

}

在UINavigationController的delegate中实现以下方法:

?

1

2

3

4

5

6
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

//开启滑动手势

if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

navigationController.interactivePopGestureRecognizer.enabled = YES;

}

}

在pushviewcontroller之前加入以下代码:

?

1

2

3

4

5
//在切换界面的过程中禁止滑动手势,避免界面卡死

if ([_currentNav respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

_currentNav.interactivePopGestureRecognizer.enabled = NO;

}

[_currentNav pushViewController:viewController animated:YES];

即可在实现滑动返回的同时,避免界面卡死的问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 iOS左滑手势失效的解决方法 https://www.kuaiidc.com/90332.html

相关文章

发表评论
暂无评论