关于侧滑interactivePopGestureRecognizer 卡住遇到的坑

类似qq的可以侧滑返回,效果不错,当你在实现的时候本来几句代码可以简洁的搞定,可是你后来就会发现一个大坑,导致的你app Crash ,crash的提示是Can't add self as subview

分析问题,根本问题的实质其实是你在执行上一个Push 或者Pop的动画期间比如0.3秒内 你又执行力Push或者Pop导致的问题出现,
经过网上查找的资料得出总结如下 上代码
1.首先你要先了解interactivePopGestureRecognizer(导航控制器的这个交互手势)其实就是方便开发者设置不同的手势和导航控制的交互设置的属性
在你的UINavigationController基类里边写下边的代码:

  • UINavigationController 基类viewDidLoad方法中加入如下代码
    -(void)viewDidLoad{
    __weak MMNavController *weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
    self.interactivePopGestureRecognizer.delegate = weakSelf;
    self.delegate = weakSelf;
    }
    id target = self.interactivePopGestureRecognizer.delegate;
    _screenEdgePanGesture = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    _screenEdgePanGesture.edges = UIRectEdgeLeft;
    _screenEdgePanGesture.delegate = self;
    self.delegate = self;
    [self.view addGestureRecognizer:_screenEdgePanGesture];
    }

  • -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer   *)gestureRecognizer{
        About91ViewController *childVC;
        if (self.childViewControllers.count == 3) {
            if ([[self.childViewControllers objectAtIndex:2] class] == [About91ViewController class]) {
                  childVC = (About91ViewController*)[self.viewControllers objectAtIndex:2];
                  if (childVC.webViewType == INVESTMENTNOW) {
                      return NO;
                  }
             }
         }
        if (self.childViewControllers.count == 1) {
            return NO;
        }else if (self.childViewControllers.count ==3 &&   [[self.viewControllers  objectAtIndex:2] isKindOfClass:[JYProductDetailVC class]]){
            return NO;
        }
        return YES;
      }
    
  • - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
         if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)]&&animated == YES ){
              self.interactivePopGestureRecognizer.enabled = NO;
              self.screenEdgePanGesture.enabled = NO;
          }
          [super pushViewController:viewController animated:animated];
    }
    
  • - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
    {
         if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)]&& animated == YES ){
              self.interactivePopGestureRecognizer.enabled = NO;
              self.screenEdgePanGesture.enabled = NO;
         }
        return [super popToRootViewControllerAnimated:animated];
    }
    
  • - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated {
            if( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] ){
                self.interactivePopGestureRecognizer.enabled = NO;
                self.screenEdgePanGesture.enabled = NO;
            }
            return [super popToViewController:viewController animated:animated];
    }
    
  • - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animate {
        if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]){
            self.interactivePopGestureRecognizer.enabled = YES;
            self.screenEdgePanGesture.enabled = YES;
      }
    }

你可能感兴趣的:(关于侧滑interactivePopGestureRecognizer 卡住遇到的坑)