zoukankan      html  css  js  c++  java
  • 侧边栏抽屉效果之开源库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];
    }

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

  • 相关阅读:
    实例使用总结
    AI vs PS 矢量 VS 位图
    快捷键总结
    总结常用公共模块
    No module named MySQLdb
    Ubuntu上mysql, 通过python连接报错Can't connect to MySQL server on xxx (10061)
    移动端Vue组件库-Vant学习
    CKEditor与定制
    js如何将一个数组按照自己固定的顺序排序?
    js如何替换字符串中匹配到多处中某一指定节点?
  • 原文地址:https://www.cnblogs.com/h-tao/p/5147253.html
Copyright © 2011-2022 走看看