• 隐藏NavigationBar 带来的坑


    一、场景介绍

      现在大多数APP 都有一个需求,就是隐藏某一个页面的NavigationBar。很多开发者直接   [self.navigationController setNavigationBarHidden:YES] 就万事大吉了。但是如果开发者试着将边缘侧滑返回功能加上之后,细心的同学就会发现,如果我们在一个隐藏NavigationBar的页面和一个显示NavigationBar 的页面通过手势来回切换后,再继续push到更深层的页面,顶部的NavigationBar就会出现错乱的情况。因此好多APP 将侧滑返回直接去掉了,但是这样的话,整个APP 就会显得比较粗糙,体验不佳。其实早已iOS自带了侧滑返回功能,只不过大多数时候由于我们自定义了NavigationBarleftBarButtonItem,导致了转场交互代理interactivePopGestureRecognizer失效了,需要我们重新指定一下代理,具体实现方式,网上其他博客有很详细的解释,在此不再赘述。

    二、问题解决

      为了更好的描述问题,我特地定义了两个控制器:1、Viewcontroller1(隐藏了导航栏,导航控制器的根控制器) ;2、Viewcontroller2(需要显示导航控制器,由Viewcontroller1 push 而来)。  那么怎么解决“侧滑返回功能”与“隐藏NavigationBar”共存呢?我的思路是:既然再同一个导航控制器栈内会出问题,那么我将Viewcontroller1和Viewcontroller2放在两个导航控制器里不就可以了吗?但是我们还想需要push的效果怎么办?导航控制器是不允许在push一个新的导航控制器的。只能present出来一个新的导航控制器。因此我想到了自定义一个转场动画,让present出来的效果如同push的效果一样。具体的代码等会会贴出,但是建议大家看一下王巍所写的转场动画这部分知识。

      1)设置侧滑返回

    #import <UIKit/UIKit.h>
    
    @protocol ModalViewControllerDelegate <NSObject>
    
    -(void) modalViewControllerDidClickedDismissButton:(UIViewController *)viewController;
    
    @end
    
    @interface BaseNavigationController : UINavigationController<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
    //@property(nonatomic,weak)UIPanGestureRecognizer *popPan;
    
    //转场代理
    @property (nonatomic, weak) id<ModalViewControllerDelegate> transDelegate;
    
    @end
    

     #import "BaseNavigationController.h"

    
    @interface BaseNavigationController ()
    
    @property(nonatomic,weak) UIViewController* currentShowVC;
    
    @end
    
    @implementation BaseNavigationController
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        //请忽略这段设置UI样式的代码,没什么卵用
        [self.navigationBar setBarTintColor:[Theme colorOne]];
        [self.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                    [UIColor whiteColor],NSForegroundColorAttributeName, nil]];
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
        [self.navigationBar setTranslucent:NO];
        
    self.view.layer.shadowColor = [UIColor blackColor].CGColor; self.view.layer.shadowOffset = CGSizeMake(-2, 0); self.view.layer.shadowOpacity = 0.3; } //导航控制器一被创建则设置交互手势代理 -(id)initWithRootViewController:(UIViewController *)rootViewController { BaseNavigationController* nvc = [super initWithRootViewController:rootViewController]; self.interactivePopGestureRecognizer.delegate = self; nvc.delegate = self; return nvc; }
    //这段主要是判断为了帮助手势判断当前响应手势的控制器是不是根视图控制器 -(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (navigationController.viewControllers.count == 1) self.currentShowVC = Nil; else if(animated){ self.currentShowVC = viewController; }else{ self.currentShowVC = Nil; } }
    //交互手势代理(告诉手势什么时候需要响应,什么时候不响应) -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer == self.interactivePopGestureRecognizer) { return (self.currentShowVC == self.topViewController); } return YES; }

     设置侧滑返回我们需要在base类里重新设置代理,并实现转场手势的代理,告诉手势当页面为根视图控制器时候手势不响应,其实以上代码就是实现这么点的功能,仅此而已。

      2)设置转场动画

    转场动画我是直接修改了王巍的代码,原文地址在这里 https://onevcat.com/2013/10/vc-transition-in-ios7/ 。以下是我修改后的代码,只是修改了动画部分,使用方法是一致的。

    #import "BouncePresentAnimation.h"
    
    @implementation BouncePresentAnimation
    - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        return 0.25f;
    }
    
    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        // 1. Get controllers from transition context
        UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        
        // 2. Set init frame for toVC
        CGRect screenBounds = [[UIScreen mainScreen] bounds];
        CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
        toVC.view.frame = CGRectOffset(finalFrame, screenBounds.size.width, 0);
        
        // 3. Add toVC's view to containerView
        UIView *containerView = [transitionContext containerView];
        [containerView addSubview:toVC.view];
        
        // 4. Do animate now
        NSTimeInterval duration = [self transitionDuration:transitionContext];
        [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            toVC.view.frame = finalFrame;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    }
    
    
    
    
    #import "NormalDismissAnimation.h"
    
    @implementation NormalDismissAnimation
    - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        return 0.25f;
    }
    
    - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        // 1. Get controllers from transition context
        UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        
        // 2. Set init frame for fromVC
        CGRect screenBounds = [[UIScreen mainScreen] bounds];
        CGRect initFrame = [transitionContext initialFrameForViewController:fromVC];
        CGRect finalFrame = CGRectOffset(initFrame, screenBounds.size.width,0 );
        
        // 3. Add target view to the container, and move it to back.
        UIView *containerView = [transitionContext containerView];
        [containerView addSubview:toVC.view];
        [containerView sendSubviewToBack:toVC.view];
    
        // 4. Do animate now
        NSTimeInterval duration = [self transitionDuration:transitionContext];
        
        [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            fromVC.view.frame = finalFrame;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
    

      3)又一个坑

    大家将转场动画和侧滑功能加入后会发现,我擦当我在ViewController2中再继续push时,如果侧滑则直接返回到了我们的ViewController1控制器,而不是我们想要的次栈顶控制器。这涉及到了手势之间竞争的问题,也就是说转场的手势取代我们边缘侧滑手势去响应了,因此我们要告诉系统,如果当前手势所在的控制器不是present出来的导航控制器的根控制器的话,转场手势就不需要响应。因此需要修改王大师的一部分代码

    #import "SwipeUpInteractiveTransition.h"
    
    @interface SwipeUpInteractiveTransition()<UIGestureRecognizerDelegate>
    @property (nonatomic, assign) BOOL shouldComplete;
    @property (nonatomic, strong) UIViewController *presentingVC;
    @end
    
    @implementation SwipeUpInteractiveTransition
    -(void)wireToViewController:(UIViewController *)viewController
    {
        self.presentingVC = viewController;
        [self prepareGestureRecognizerInView:viewController.view];
    }
    
    - (void)prepareGestureRecognizerInView:(UIView*)view {
        UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
        gesture.delegate = self;
        [view addGestureRecognizer:gesture];
    }
    
    -(CGFloat)completionSpeed
    {
        return 1 - self.percentComplete;
    }
    
    - (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer {
        CGPoint translation = [gestureRecognizer translationInView:gestureRecognizer.view.superview];
           switch (gestureRecognizer.state) {
            case UIGestureRecognizerStateBegan:
                // 1. Mark the interacting flag. Used when supplying it in delegate.
                self.interacting = YES;
                [self.presentingVC dismissViewControllerAnimated:YES completion:nil];
                break;
            case UIGestureRecognizerStateChanged: {
                // 2. Calculate the percentage of guesture
                CGSize screenSize = [UIScreen mainScreen].bounds.size;
                CGFloat fraction = translation.x / screenSize.width;
                //Limit it between 0 and 1
                fraction = fminf(fmaxf(fraction, 0.0), 1.0);
                self.shouldComplete = (fraction > 0.5);
    
                [self updateInteractiveTransition:fraction];
                break;
            }
            case UIGestureRecognizerStateEnded:
            case UIGestureRecognizerStateCancelled: {
                // 3. Gesture over. Check if the transition should happen or not
                self.interacting = NO;
                if (!self.shouldComplete || gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
                    [self cancelInteractiveTransition];
                } else {
                    [self finishInteractiveTransition];
                }
                break;
            }
            default:
                break;
        }
    }
    
    //如果不是根视图控制器则不响应
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
        if ([self.presentingVC isKindOfClass:[BaseNavigationController class]]) {
            BaseNavigationController *nav = (BaseNavigationController*)self.presentingVC;
            if (nav.viewControllers.count >=2) {
                return NO;
            }
        }
        return YES;
    }
    

    至此侧滑和导航栏的隐藏则完美兼容,希望这篇文章对你有用~

  • 相关阅读:
    [转]lftp的致命错误:证书验证:不信任
    github每次push都需要密码以及用户名的解决办法
    Fedora最小化安装后没有ifconfig命令
    [转载]MySql常用命令总结
    chrome浏览器强制采用https加密链接
    红帽系列linux自行配置本地yum源
    linux 下dd命令直接清除分区表(不用再fdisk一个一个的删除啦)
    linux分区工具fdisk的使用
    Java多线程实现......(1,继承Thread类)
    第一篇文章--我为什么要写博客?
  • 原文地址:https://www.cnblogs.com/dengzhuli/p/5326775.html
Copyright © 2020-2023  润新知