如何禁止手机侧滑返回上一个页面

如何禁止手机侧滑返回上一个页面。 侧滑返回上一个页面用户体验很很好。但是在特别的场景侧滑返回上一个页面严重影响用户体验: 1.如正在斗地主,结果在出牌时,由于出最左边的牌时,不小心触发了侧滑离开斗地主页面。你说烦不烦。更离谱的是有的app,这个斗地主是随机的。你离开后再想进入继续在原来的斗地主,结果发现没有斗地主的入口。结果只能由系统托管了。你说烦不烦。所以这样的页面摇禁止侧滑返回上一个页面,而是通过在左上交点击退出返回上一层页面。 2.在登陆页面,结果由于侧滑导致不登陆就返回原来的页面出现各种报错。所以登陆页面也要设置侧滑放回上一个页面。 一般的页面只需要这样设置就可以禁止侧滑返回上一个页面。

- (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;
    }
}
复制代码

问题是由的页面这样设置无效,仍旧设置。那么只能来猛药了。找到弹出手势时间,禁止它,离开时设置放开它。

  • (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated];

    [self.playerView startPlaying]; [self popGestureChange:self enable:NO]; // // 开启返回手势 // if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { // self.navigationController.interactivePopGestureRecognizer.delegate = self; // } } -(void)popGestureChange:(UIViewController *)vc enable:(BOOL)enable{

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

      //遍历所有的手势
    
      for (UIGestureRecognizer *popGesture in vc.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
    
          popGesture.enabled = enable;
    
      }
    复制代码

    }

}

  • (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [self.playerView pausePlaying]; [self popGestureChange:self enable:YES]; // // 开启返回手势 // if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { // self.navigationController.interactivePopGestureRecognizer.delegate = nil; // } self.navigationController.navigationBar.hidden = NO; }
复制代码
分类:
iOS
标签: