iOS 收集专题iOS 移动端开发实用小功能

iOS-MMDrawerController的使用

2016-03-22  本文已影响13668人  LitterL

说明:

最近在项目中需要实现一个抽屉效果,后来查了查资料觉得MMDrawerController还是比较不错的,下载下来试了一试MMDrawerController顺便做了一个小Demo,下图(希望能帮助大家):

image

这里的话我是直接使用的cocoapods,我就不详细介绍了,这里我就直接说Demo了,使用为纯代码

代码:

1、首先创建三个控制器为center、left、right(我这里就简写了,具体的话大家可以下载Demo),创建完成之后,我们来到我们的AppDelegate,开始编写我们的代码了

   //为MMDrawerController框架中
   #import "MMDrawerController.h"
   #import "UIViewController+MMDrawerController.h"

   //为自己创建的三个控制器
   #import "LitterLCenterViewController.h"
   #import "LitterLLeftViewController.h"
   #import "LitterLRightViewController.h"

   @interface LitterLAppDelegate ()
   /**
    *  MMDrawerController属性
    */
   @property(nonatomic,strong) MMDrawerController * drawerController;
   @end
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

   //1、初始化控制器
   UIViewController *centerVC = [[LitterLCenterViewController alloc]init];
   UIViewController *leftVC = [[LitterLLeftViewController alloc]init];
   UIViewController *rightVC = [[LitterLRightViewController alloc]init];
   //2、初始化导航控制器
   UINavigationController *centerNvaVC = [[UINavigationController alloc]initWithRootViewController:centerVC];
   UINavigationController *leftNvaVC = [[UINavigationController alloc]initWithRootViewController:leftVC];
   UINavigationController *rightNvaVC = [[UINavigationController alloc]initWithRootViewController:rightVC];

   //3、使用MMDrawerController
   self.drawerController = [[MMDrawerController alloc]initWithCenterViewController:centerNvaVC leftDrawerViewController:leftNvaVC rightDrawerViewController:rightNvaVC];

   //4、设置打开/关闭抽屉的手势
   self.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
   self.drawerController.closeDrawerGestureModeMask =MMCloseDrawerGestureModeAll;
   //5、设置左右两边抽屉显示的多少
   self.drawerController.maximumLeftDrawerWidth = 200.0;
   self.drawerController.maximumRightDrawerWidth = 200.0;

   //6、初始化窗口、设置根控制器、显示窗口
   self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
   [self.window setRootViewController:self.drawerController];
   [self.window makeKeyAndVisible];
   return YES;
}

2、完成上面后基本的抽屉效果就已经实现了,在这里的话,我们将要实现导航栏上面的按钮,以及一些效果。

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBtn)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"navigationbar_friendattention"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBtn)];
//首先得导入头文件
#import "MMDrawerBarButtonItem.h"
//----------------------------
self.navigationItem.leftBarButtonItem = [[MMDrawerBarButtonItem alloc]initWithTarget:self action:@selector(leftBtn)];
self.navigationItem.rightBarButtonItem = [[MMDrawerBarButtonItem alloc]initWithTarget:self action:@selector(rightBtn)];
//首先得导入头文件
#import "UIViewController+MMDrawerController.h"
//----------------------------
-(void)leftBtn{
//这里的话是通过遍历循环拿到之前在AppDelegate中声明的那个MMDrawerController属性,然后判断是否为打开状态,如果是就关闭,否就是打开(初略解释,里面还有一些条件)
  [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
-(void)rightBtn{
  [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
   //2、添加双击手势
   UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
   //2.1、双击
   [doubleTap setNumberOfTapsRequired:2];
   [self.view addGestureRecognizer:doubleTap];

   //3、添加两个手指双击手势
   UITapGestureRecognizer * twoFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerDoubleTap:)];
   //3.1、双击
   [twoFingerDoubleTap setNumberOfTapsRequired:2];
   //3.2、两个手指  默认为一个
   [twoFingerDoubleTap setNumberOfTouchesRequired:2];
   [self.view addGestureRecognizer:twoFingerDoubleTap];

   //----------------------------
   /**
    *  添加点击手势  一个手指双击
    */
   -(void)doubleTap:(UITapGestureRecognizer*)gesture{
       [self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
   }

   /**
    *  添加点击手势  两个个手指双击
    */
   -(void)twoFingerDoubleTap:(UITapGestureRecognizer*)gesture{
       [self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideRight completion:nil];
   }

3、到这里的话就是最后一步了,第一设置数据源,第二实现协议了。

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text =[NSString stringWithFormat:@"Left-Demo%ld",indexPath.row];
    return cell;
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LitterLShowViewController *showVC = [[LitterLShowViewController alloc]init];
    showVC.title = [NSString stringWithFormat:@"Left-Demo%ld",indexPath.row];

    //拿到我们的LitterLCenterViewController,让它去push
    UINavigationController* nav = (UINavigationController*)self.mm_drawerController.centerViewController;
    [nav pushViewController:showVC animated:NO];
    //当我们push成功之后,关闭我们的抽屉
    [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
        //设置打开抽屉模式为MMOpenDrawerGestureModeNone,也就是没有任何效果。
        [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
    }];
}

4、差点忘了一个,就是当我们的LitterLShowViewController退出后,我们的把打开抽屉模式在切换过来,当然这个是在中间控制器里面去写,因为LitterLShowViewController退出后会呈现中间控制器

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

Demo地址:MMDrawerControllerDemo

本章到此结束
欢迎各位码友随意转载并指正

上一篇下一篇

猜你喜欢

热点阅读