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];
        
    }
  • 相关阅读:
    “XXXXX” is damaged and can’t be opened. You should move it to the Trash 解决方案
    深入浅出 eBPF 安全项目 Tracee
    Unity3d开发的知名大型游戏案例
    Unity 3D 拥有强大的编辑界面
    Unity 3D物理引擎详解
    Unity 3D图形用户界面及常用控件
    Unity 3D的视图与相应的基础操作方法
    Unity Technologies 公司开发的三维游戏制作引擎——Unity 3D
    重学计算机
    windows cmd用户操作,添加,设备管理员组,允许修改密码
  • 原文地址:https://www.cnblogs.com/h-tao/p/5099512.html
Copyright © 2011-2022 走看看