美文网首页iOS项目框架搭建收藏ios
iOS开发: 仿京东导航控制器push和pop时, 导航条效果

iOS开发: 仿京东导航控制器push和pop时, 导航条效果

作者: 伯wen | 来源:发表于2017-01-07 16:59 被阅读971次
    • 在开发过程中, 需要做一个类似于京东手机app里面, 导航控制器push和pop的效果, 在网上找了很久, 都没有找到类似的方法
      当然也不是一无所获, 倒也发现了一种实现push和pop都带层次的效果, 但并不是我想要的。

    • 我需要的是push过程为普通样式, pop的时候才有层次结构
      最后, 在朋友的帮助下, 完成了下面的效果:

    • 慢动作:


    • 正常速度:


    实现思路

    1. push的效果必须是普通的, 所以在push过程中, 不能做任何操作
    2. pop的效果是导航条跟着控制器走
    3. 所以在push过程中, 导航条一直显示, 而在pop过程中使用动画的方式隐藏导航条
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        // 动画效果隐藏导航条
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    

    就会出现如下效果:


    • 结果: push的效果和pop的效果已经实现了, 但是有一个问题, 那就是pop过程中, 前一个界面的导航条被隐藏了

    • 所以我们还需要一个操作, 那就是要在pop过程中, 前一个界面中显示导航条才行, 这个问题想了很久, 最后决定使用截屏的方式实现效果

    • 过程:

      • 在整个push的时候, 控制器将要消失的方法里截取屏幕, 并添加到根视图上, 这里需要显示在根视图的最上面
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0);
    [self.navigationController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.screenshots.hidden = NO;
    self.screenshots.image = image;
    [self.view bringSubviewToFront:self.screenshots];
    
    • 这里的screenshots, 是一个UIImageView对象, 用来显示截取的图片
    • 当添加完截图后, 就有了这样的现象
    • 效果看起来已经实现了, 但是却有一个问题, 那就是第一个界面上显示的只是一张截图, 并没有导航条, 并且截图是无法操作业务的
    • 所以在截图完成后, 我们需要将导航条显示出来, 由于截取的图片上的导航条和实际导航条是一样的, 所以这里直接显示导航条而不通过动画, 并且隐藏掉截图
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [self.navigationController setNavigationBarHidden:NO animated:NO];
        self.screenshots.hidden = YES;
    }
    

    最后

    通过这种方式, 就实现了模仿京东导航条的效果

    下面是我写的一个UIViewController分类, 可以直接使用, 如果有什么地方质量不够好请留言, 我会修改

    .h文件

    #import <UIKit/UIKit.h>
    
    /**
     导航控制器的状态
    
     - LTNavigationControllerStatusNone: 无导航控制器, 或则是 导航控制器的跟控制器
     - LTNavigationControllerStatusPush: 正在push
     - LTNavigationControllerStatusPop: 正在pop
     LTNavigationControllerStatusNotApply: 不使用
     */
    typedef NS_ENUM(NSInteger, LTNavigationControllerStatus) {
        LTNavigationControllerStatusNone,
        LTNavigationControllerStatusPush,
        LTNavigationControllerStatusPop,
        LTNavigationControllerStatusNotApply
    };
    
    @interface UIViewController (LTExchangeMethod)
    
    /** 截图 */
    @property (nonatomic, strong) UIImageView *screenshots;
    
    /** 当前控制器的导航控制器状态 */
    @property (nonatomic, assign) LTNavigationControllerStatus navigationControllerStatus;
    
    @end
    

    .m文件

    #import "UIViewController+LTExchangeMethod.h"
    #import <objc/runtime.h>
    
    static const NSString *screenshotsKey = @"screenshots";
    static const NSString *navigationControllerStatusKey = @"navigationControllerStatus";
    @implementation UIViewController (LTExchangeMethod)
    
    + (void)load
    {
        // 加载过程中, 交换方法
        
        //控制器已经出现
        Method viewDidAppear = class_getInstanceMethod(self, @selector(viewDidAppear:));
        Method lt_viewDidAppear = class_getInstanceMethod(self, @selector(lt_viewDidAppear:));
        method_exchangeImplementations(viewDidAppear, lt_viewDidAppear);
        
        //控制器即将消失
        Method viewWillDisappear = class_getInstanceMethod(self, @selector(viewWillDisappear:));
        Method lt_viewWillDisappear = class_getInstanceMethod(self, @selector(lt_viewWillDisappear:));
        method_exchangeImplementations(viewWillDisappear, lt_viewWillDisappear);
        
        //控制器已经消失
        Method viewDidDisappear = class_getInstanceMethod(self, @selector(viewDidDisappear:));
        Method lt_viewDidDisappear = class_getInstanceMethod(self, @selector(lt_viewDidDisappear:));
        method_exchangeImplementations(viewDidDisappear, lt_viewDidDisappear);
    }
    
    /**
     控制器即将消失
     */
    - (void)lt_viewWillDisappear:(BOOL)animation
    {
        UIViewController *viewController = self.navigationController.childViewControllers.lastObject;
        if (viewController.navigationControllerStatus == LTNavigationControllerStatusPop) {
            [self.navigationController setNavigationBarHidden:YES animated:YES];
        }else if (viewController.navigationControllerStatus == LTNavigationControllerStatusPush) {
            if (self.screenshots.hidden == NO) return;
            UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, NO, 0.0);
            [self.navigationController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            self.screenshots.image = image;
            [self.view bringSubviewToFront:self.screenshots];
            self.screenshots.hidden = NO;
        }
        [self lt_viewWillDisappear:animation];
    }
    
    /**
     控制器已经消失
     */
    - (void)lt_viewDidDisappear:(BOOL)animation
    {
        if (self.navigationControllerStatus == LTNavigationControllerStatusPop) {
            self.screenshots.hidden = NO;
        }
        [self lt_viewDidDisappear:animation];
    }
    
    /**
     控制器已经出现
     */
    - (void)lt_viewDidAppear:(BOOL)animation
    {
        if (self.navigationControllerStatus == LTNavigationControllerStatusPop) {
            [self.navigationController setNavigationBarHidden:NO animated:NO];
            self.screenshots.hidden = YES;
        }
        [self lt_viewDidAppear:animation];
    }
    
    #pragma mark - < 截图 >
    
    - (void)setScreenshots:(UIImageView *)screenshots
    {
        objc_setAssociatedObject(self, (__bridge const void *)(screenshotsKey), screenshots, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (UIImageView *)screenshots
    {
        UIImageView *screenshots = objc_getAssociatedObject(self, (__bridge const void *)(screenshotsKey));
        if (!screenshots) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            imageView.hidden = YES;
            [self.view addSubview:imageView];
            self.screenshots = imageView;
            screenshots = imageView;
        }
        return screenshots;
    }
    
    #pragma mark - < 当前控制器的导航控制器状态 >
    
    - (void)setNavigationControllerStatus:(LTNavigationControllerStatus)navigationControllerStatus
    {
        objc_setAssociatedObject(self, (__bridge const void *)(navigationControllerStatusKey), @(navigationControllerStatus), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (LTNavigationControllerStatus)navigationControllerStatus
    {
        NSNumber *navigationControllerStatus = objc_getAssociatedObject(self, (__bridge const void *)(navigationControllerStatusKey));
        return navigationControllerStatus.integerValue;
    }
    
    @end
    
    @interface UINavigationController (LTExchangeMethod)
    
    @end
    
    @implementation UINavigationController (LTExchangeMethod)
    
    + (void)load
    {
        //控制器已经出现
        Method pushViewController = class_getInstanceMethod(self, @selector(pushViewController:animated:));
        Method lt_pushViewController = class_getInstanceMethod(self, @selector(lt_pushViewController:animated:));
        method_exchangeImplementations(pushViewController, lt_pushViewController);
    }
    
    - (void)lt_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
        if (self.childViewControllers.count > 0) {
            if (viewController.navigationControllerStatus != LTNavigationControllerStatusNotApply) {
                viewController.navigationControllerStatus = LTNavigationControllerStatusPush;
                self.childViewControllers.lastObject.navigationControllerStatus = LTNavigationControllerStatusPop;
            }
        }
        [self lt_pushViewController:viewController animated:animated];
    }
    
    @end;
    

    使用方法: 写一个分类保存, 直接拖入工程中即可, 不需引用自动生效

    <a>如果代码有问题请留言</a>
    <a>禁止转载</a>

    相关文章

      网友评论

      • 砂缚柩:bug
        尝试手势返回,就来回滑动,距离越小越好
        导航栏直接就被挤掉了
      • 留几分念想:如果横屏的话好像有点问题,你看一下
      • Leon_520:学习了.

      本文标题:iOS开发: 仿京东导航控制器push和pop时, 导航条效果

      本文链接:https://www.haomeiwen.com/subject/ohufbttx.html