• 侧边栏抽屉效果之开源库MMDrawerController的使用


    1、开源库,利用pod引入第三方库MMDrawerController:

      在建立的Podfile中加入代码:

    platform :ios, '7.0'
    pod 'ReactiveCocoa'
    pod 'MMDrawerController'
    pod 'Toast', '~> 3.0'
    pod 'CocoaSecurity'

      其中MMDrawerController就是开源的侧边栏库

    2、在appdelegate中加入以下代码

      首先加入头文件

    #import "AppDelegate.h"
    #import <MMDrawerController.h>
    #import "LeftViewController.h"
    #import "ViewController.h"
    #import "RightViewController.h"

      新建属性

    @property(nonatomic,assign) BOOL ifNavFromLeftMenu;
    @property(nonatomic,assign) BOOL isNavFinished;
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        ViewController *vc = [[ViewController alloc] init];
        LeftViewController *leftVc = [[LeftViewController alloc] init];
        RightViewController *right = [[RightViewController alloc] init];
        UINavigationController *center     = [[UINavigationController alloc] initWithRootViewController:vc];
        center.delegate = (id<UINavigationControllerDelegate>)self;
        
        MMDrawerController * drawerController = [[MMDrawerController alloc]
                                                 initWithCenterViewController:center
                                                 leftDrawerViewController:leftVc
                                                 rightDrawerViewController:right];
        [drawerController setShowsShadow:YES];
        drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
        [drawerController setRestorationIdentifier:@"MMDrawer"];
        [drawerController setMaximumLeftDrawerWidth:270.0];
        
        self.window.rootViewController = drawerController;
        
        return YES;
    }

      在加入左右侧边栏实现的代理方法

    -(void)navigateToView:(UIViewController*)controler ifLeft:(BOOL)ifLeft animate:(BOOL)animate
    {
        MMDrawerController*menuVctler = (MMDrawerController *)self.window.rootViewController;
        UINavigationController* nav = (UINavigationController*)menuVctler.centerViewController;
        if (nav) {
            nav.delegate = (id<UINavigationControllerDelegate>)self;
            self.ifNavFromLeftMenu = ifLeft;
            if (ifLeft) {
                [nav pushViewController:controler animated:NO];
                [menuVctler closeDrawerAnimated:YES completion:^(BOOL finished) {
                    
                }];
            }else{
                [nav pushViewController:controler animated:animate];
            }
        }
    }
    
    #pragma mark - UINavigationControllerDelegate
    - (void)navigationController:(UINavigationController *)navigationController
          willShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {
        if (!self.ifNavFromLeftMenu) {
            return;
        }
        
        UIViewController* rootVc = [navigationController.viewControllers objectAtIndex:0];
        if([viewController isEqual:rootVc]){
            MMDrawerController *menuVctler = (MMDrawerController *)self.window.rootViewController;
            __weak UINavigationController*nav = navigationController;
            __weak typeof(self)wself = self;
            [menuVctler openDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
                nav.delegate = nil;
                self.ifNavFromLeftMenu = NO;
                
                double delayInSeconds = 0.2;
                dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
                dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                    wself.isNavFinished  = YES;
                });
            }];
        }
    }
    
    - (void)navigationController:(UINavigationController *)navigationController
           didShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {
        if (!self.ifNavFromLeftMenu) {
            return;
        }
    }

    3、在主控制器中加入导航栏左右按钮的点击处理实现方法:

    -(void)leftDrawerButtonPress:(id)sender{
        [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
    }
    -(void)rightDrawerButtonPress:(id)sender{
        [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
    }

    编译运行就可以实现左右侧边栏的抽屉效果。

  • 相关阅读:
    四月书籍
    iOS 第三方登录之 QQ登录
    百度面试经验之谈 ----------转
    OC浅析一
    书目列表
    UIStroryboard分类
    ios基础笔记(一)
    IOS笔记-代码块(微博项目)-1.0
    IOS控件内容 对齐方式
    android同一个TextView设置不同颜色字体
  • 原文地址:https://www.cnblogs.com/h-tao/p/5147253.html
Copyright © 2020-2023  润新知