gihub框架地址:https://github.com/mutualmobile/MMDrawerController
将MMDrawerController框架导入工程中
实现效果如下:
侧滑1、首先创建2个控制器为center、left,创建完成之后,我们来到我们的AppDelegate,开始编写我们的代码了
1.1多话不说,先导入头文件,并且添加一个MMDrawerController的属性
#import "MMDrawerController.h"
#import "UIViewController+MMDrawerController.h"
//为自己创建的控制器
#import "LitterLLeftViewController.h"
#import "LitterLCenterViewController.h"
// 添加一个MMDrawerController的属性
@property(nonatomic,strong)MMDrawerController *drawerController;
1.2 上面的做完后,我们便要显示我们的窗口到设备上,接下来来到这里
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//1、初始化控制器
UIViewController *centerVC = [[LitterLCenterViewController alloc]init];
UIViewController *leftVC = [[LitterLLeftViewController alloc]init];
//2、初始化导航控制器
UINavigationController *centerNvaVC = [[UINavigationController alloc]initWithRootViewController:centerVC];
UINavigationController *leftNvaVC = [[UINavigationController alloc]initWithRootViewController:leftVC];
//3、使用MMDrawerController
self.drawerController = [[MMDrawerController alloc]initWithCenterViewController:centerNvaVC leftDrawerViewController:leftNvaVC];
//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、完成上面后基本的抽屉效果就已经实现了,在这里的话,我们来到我们的中间控制器里,我们将要实现导航栏上面的按钮,以及一些效果。
// 导入头文件
#import "UIViewController+MMDrawerController.h"
/**
* 加载控制器的时候设置打开抽屉模式 (因为在后面会关闭)
*/
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//设置打开抽屉模式
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
}
self.view.backgroundColor = [UIColor whiteColor];
self.title=@"Demo";
//1、设置导航栏的按钮
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBtn)];
//2、添加双击手势
UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
//2.1、双击
[doubleTapsetNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleTap];
//3、添加两个手指双击手势
UITapGestureRecognizer* twoFingerDoubleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(twoFingerDoubleTap:)];
//3.1、双击
[twoFingerDoubleTapsetNumberOfTapsRequired:2];
//3.2、两个手指 默认为一个
[twoFingerDoubleTapsetNumberOfTouchesRequired:2];
[self.viewaddGestureRecognizer:twoFingerDoubleTap];
-(void)leftBtn{
//这里的话是通过遍历循环拿到之前在AppDelegate中声明的那个MMDrawerController属性,然后判断是否为打开状态,如果是就关闭,否就是打开(初略解释,里面还有一些条件)
[self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
/**
* 添加点击手势 一个手指双击
*/
-(void)doubleTap:(UITapGestureRecognizer*)gesture{
[self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
}
/**
* 添加点击手势 两个个手指双击
*/
-(void)twoFingerDoubleTap:(UITapGestureRecognizer*)gesture{
[self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideRight completion:nil];
}
3.1、数据源的编写,我们来到我们的左边的控制器里,这里的话我用的都是静态数据,就不做解释了
// 导入头文件
#import "LitterLShowViewController.h" // 点击单元格跳转到这个控制器上
#import "UIViewController+MMDrawerController.h"
self.title = @"Left-Demo";
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
tableView.delegate=self;
tableView.dataSource=self;
[self.viewaddSubview:tableView];
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
staticNSString*CellIdentifier =@"Cell";
UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text=[NSStringstringWithFormat:@"Left-Demo%ld",indexPath.row];
returncell;
}
#pragma mark - UITableViewDelegate
// 3.2、这里就是我们的最后一步,点击Cell跳转控制器了,那么我们的有一个控制器取名为:LitterLShowViewController
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
LitterLShowViewController *showVC = [[LitterLShowViewController alloc]init];
showVC.title= [NSStringstringWithFormat:@"Left-Demo%ld",indexPath.row];
//拿到我们的LitterLCenterViewController,让它去push
UINavigationController* nav = (UINavigationController*)self.mm_drawerController.centerViewController;
[navpushViewController:showVC animated:NO];
//当我们push成功之后,关闭我们的抽屉
[self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
//设置打开抽屉模式为MMOpenDrawerGestureModeNone,也就是没有任何效果。
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
}];
}
4、当我们的LitterLShowViewController退出后,我们的把打开抽屉模式在切换过来,当然这个是在中间控制器里面去写,因为LitterLShowViewController退出后会呈现中间控制器
// 导入头文件
#import "MMDrawerBarButtonItem.h"
#import "UIViewController+MMDrawerController.h"
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
//关闭抽屉模式
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
}
网友评论