IOS-MMDrawerController左边抽屉push的三

作者: 温学振 | 来源:发表于2018-01-04 12:09 被阅读306次

    总结了大部分的界面 大概就如下三种情况:

    1. 打开左边抽屉,按钮后左边push到center控制器,这个控制器直接显示全部
      (类似知乎日报)
      思路:设置left的抽屉为center的nav并且关闭抽屉

    2. 打开左边抽屉,按钮后左边push出一个新控制器,但是这个控制器直接显示全部
      (类似qq)
      思路:本质是用中间控制器的nav去push到对应的控制器 并且关闭中间控制器的push动画 然后再同时关闭抽屉 这样子会造成假象

    3. 打开左边抽屉,按钮后左边push出一个新控制器,但是这个控制器仍然是显示左边抽屉里的
      (我没见过由这么奇怪的需求)
      思路:直接设置push

      MMDrawerController.gif

    上代码

    1. pod 'MMDrawerController'
    2. 在AppDelegate中
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.mainController = [[mainController alloc] init];
        self.window.rootViewController = self.mainController;
        [self.window makeKeyAndVisible];
        
        return YES;
    }
    

    为了不对AppDelegate有过多的入侵 我们将继承自MMDrawerController的mainController作为根控制器

    1. 在mainController.h中
    //这里将centerNavVc写成public的接口方式 方便日后被拿到
    @property (nonatomic,strong)UINavigationController * centerNavVc;
    

    4.在mainController.m中

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor yellowColor];
        
        
        //初始化
        LeftViewController *leftVC = [[LeftViewController alloc] init];
        //将leftVC作为leftNvaVC的根控制器
        UINavigationController *leftNvaVC = [[UINavigationController alloc] initWithRootViewController:leftVC];
        //将mainController作为赋值给leftVC
        leftVC.delegate = self;
        
        CenterViewController *centerVC = [[CenterViewController alloc] init];
        self.centerNavVc = [[UINavigationController alloc] initWithRootViewController:centerVC];
    
        //设置左中抽屉内是什么东西
        self.centerViewController = self.centerNavVc;
        self.leftDrawerViewController = leftNvaVC;
    
        //4、设置打开/关闭抽屉的手势
        self.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
        self.closeDrawerGestureModeMask =MMCloseDrawerGestureModeAll;
        //5、设置左边抽屉显示的多少
        self.maximumLeftDrawerWidth = 200.0;
    }
    

    其实我们是将left的Nav和center的Nav添加到左中抽屉内,并且要在左边抽屉做响应的话leftVC.delegate = self 一定要写

    1. LeftViewController.h中
    @property (nonatomic,strong) mainController *delegate;
    

    方便取到mainController内的centerNavVc

    1. LeftViewController.m中
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor redColor];
        
        self.title = @"left";
        //push到center
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 176, 50, 50)];
        btn.backgroundColor = [UIColor yellowColor];
        [btn addTarget:self action:@selector(Click1) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
        //push到新的控制器 在center中
        UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 176+50, 50, 50)];
        btn1.backgroundColor = [UIColor brownColor];
        [btn1 addTarget:self action:@selector(click2) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn1];
    
        //left push
        UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 176+50+50, 50, 50)];
        btn2.backgroundColor = [UIColor blackColor];
        [btn2 addTarget:self action:@selector(Click3) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn2];
        
    }
    

    初始化

    /**
     *  加载控制器的时候设置打开抽屉模式  (因为在后面会关闭)
     */
    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        //设置打开抽屉模式
        [self.delegate setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    }
    - (void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
    }
    

    要打开抽屉的手势模式,不然不能在后面不能使用

    /push到center
    - (void)Click1{
        NSLog(@"%s",__func__);
        CenterViewController *vc = [[CenterViewController alloc] init];
        UINavigationController *navcenter = [[UINavigationController alloc] initWithRootViewController:vc];
        [self.delegate setCenterViewController:navcenter withCloseAnimation:YES completion:nil];
    }
    

    设置left的抽屉为center的nav并且关闭抽屉

    //push到新的控制器 在center中
    - (void)click2{
        NSLog(@"%s",__func__);
        threeViewController *showVC = [[threeViewController alloc] init];
        //拿到我们的LitterLCenterViewController,让它去push
        UINavigationController* nav = (UINavigationController*)self.delegate.centerViewController;
        [nav pushViewController:showVC animated:NO];
        //当我们push成功之后,关闭我们的抽屉
        [self.delegate closeDrawerAnimated:YES completion:^(BOOL finished) {
            //设置打开抽屉模式为MMOpenDrawerGestureModeNone,也就是没有任何效果。
            [self.delegate setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
        }];
    }
    

    本质是用中间控制器的nav去push到对应的控制器 并且关闭中间控制器的push动画 然后再同时关闭抽屉 这样子会造成假象

    //left push
    - (void)Click3{
        NSLog(@"%s",__func__);
        threeViewController *vc = [[threeViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    

    直接拿到nav进行push

    1. CenterViewController.h没有操作
    2. CenterViewController.m
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor greenColor];
        self.title = @"center";
       
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 176, 50, 50)];
        btn.backgroundColor = [UIColor yellowColor];
        [btn addTarget:self action:@selector(Click1) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
    }
    /**
     *  加载控制器的时候设置打开抽屉模式  (因为在后面会关闭)
     */
    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        //设置打开抽屉模式 
        [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    }
    
    -(void)Click1{ //打开左边抽屉
        //这里的话是通过遍历循环拿到之前在AppDelegate中声明的那个MMDrawerController属性,然后判断是否为打开状态,如果是就关闭,否就是打开(初略解释,里面还有一些条件)
        [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
    }
    
    

    回到center的时候要打开抽屉的手势 不然不能够打开左边抽屉
    打开左边抽屉的方法

    1. threeViewController.h 没有操作
    2. threeViewController.m
    -(void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        //关闭抽屉模式 push到该控制器时要关闭抽屉模式 不然会以此为center控制器打开左边抽屉
        [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    }
    
    1. end

    相关文章

      网友评论

        本文标题:IOS-MMDrawerController左边抽屉push的三

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