iOS学习

iOS禁用边缘测滑返回和页面中间右滑返回

2018-08-14  本文已影响0人  凯文Kevin21

前言: 最近在做项目的时候遇到了页面测滑返回的问题, 最后的解决办法是: 在特定的页面去禁用掉测滑返回手势。

方法一:

自定义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,会有要求某一个界面不能侧滑返回。这里给大家介绍一种简单的实现方法:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

ps: 请看好方法分别是在哪个生命周期调用的!一定不要写错方法了

上一篇 下一篇

猜你喜欢

热点阅读