zoukankan      html  css  js  c++  java
  • 左右侧边栏抽屉效果

    首先用pod引入第三方库:

    MMDrawerController

    在podfile中填写

    platform :ios, '7.0'

    pod 'MMDrawerController'

    在AppDelegate中引入头文件

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

    直接粘贴代码

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        
        
    //    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        
        
        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;
        }
    }
    @interface AppDelegate ()
    @property(nonatomic,assign) BOOL ifNavFromLeftMenu;

     @property(nonatomic,assign) BOOL isNavFinished;

    @end

    在controller中粘贴代码,首先引入头文件:

    #import <MMDrawerController.h>
    #import <MMDrawerBarButtonItem.h>
    #import "UIViewController+MMDrawerController.h"
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        [self setupLeftMenuButton];
        [self swipe];
        [self setupRightMenuButton];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(100, 100, 100, 100);
        [button setTitle:@"跳转" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        
    }
    
    
    - (void)swipe{
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(leftDrawerButtonPress:)];
        
        swipe.direction = UISwipeGestureRecognizerDirectionRight;
        
        [self.view addGestureRecognizer:swipe];
    }
    
    -(void)setupLeftMenuButton{
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 40, 40);
        [button setTitle:@"sdj" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.leftBarButtonItem = rightItem;
        
    }
    
    -(void)setupRightMenuButton{
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 40, 40);
        [button setTitle:@"sdj" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.rightBarButtonItem = rightItem;
        
    }
    
    -(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];
    }
    - (IBAction)btnClick:(id)sender {
        
        NSLog(@"sfdgh");
        MyViewController *my = [[MyViewController alloc] init];
        [self.navigationController pushViewController:my animated:YES];
        
    }
  • 相关阅读:
    C# Note23: 如何自定义类型使用foreach循环
    C# Note22: 《Effective C#》笔记
    C# Note21: 扩展方法(Extension Method)及其应用
    C# Note20: 制作延时改变显示的标题栏
    C# Note19: Windows安装包制作实践
    Python Note1: Pycharm的安装与使用
    java Html&JavaScript面试题:HTML 的 form 提交之前如何验证数值文本框的内容全部为数字? 否则的话提示用户并终止提交?
    java Html&JavaScript面试题:用table显示n条记录,每3行换一次颜色,即1,2,3用红色字体,4,5,6用绿色字体,7,8,9用红颜色字体。
    java Html&JavaScript面试题:判断第二个日期比第一个日期大
    java算法面试题:金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。
  • 原文地址:https://www.cnblogs.com/h-tao/p/5099512.html
Copyright © 2011-2022 走看看