前言: 最近在做项目的时候遇到了页面测滑返回的问题, 最后的解决办法是: 在特定的页面去禁用掉测滑返回手势。
方法一:
自定义XZWNavigationViewController 继承自 UINavigationController
在XZWNavigationViewController.m 的方法里加上这2个方法:
- (void)viewDidLoad {
[super viewDidLoad];
id target = self.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:target action:@selector(handleNavigationTransition:)];
[self.view addGestureRecognizer:pan];
//干掉系统的边缘滑动手势
self.interactivePopGestureRecognizer.enabled = NO;
pan.delegate = self;
}
#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
//出于某种需求或者避免某个bug,会有要求某一个界面不能侧滑返回,不然这里会出现一些难以解决的问题.
for (UIViewController *vc in self.childViewControllers) {
if ([vc isKindOfClass:[XZLotteryNextController class]]) {//XZLotteryNextController 特定的界面
return NO;
}
}
//判断是不是当前的跟控制器
return self.childViewControllers.count > 1;
}
方法二:
开发中,出于某种需求或者避免某个bug,会有要求某一个界面不能侧滑返回。这里给大家介绍一种简单的实现方法:
- 进入该页面时,关闭侧滑手势, 记住一定是viewDidAppear方法 不是viewWillAppear:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
- 进入下个页面或者返回上一个页面时,启用侧滑手势,
记住是viewWillDisappear方法, 不是viewDidDisappear:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
ps: 请看好方法分别是在哪个生命周期调用的!一定不要写错方法了
网友评论