我这里使用的是cocoapods来安装的MMDrawerController库,细节不再详述。
在加载本视图控制器的时候,由于其它视图控制器的切换可能会关闭 通过手势滑动打开抽屉的模式,因此需要设置一下:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
}
UIBarButtonItem* leftBarBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pressLeftBtn)];
self.navigationItem.leftBarButtonItem = leftBarBtnItem;
UIBarButtonItem* rightBarBtnItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(pressRightBtn)];
self.navigationItem.rightBarButtonItem = rightBarBtnItem;
UITapGestureRecognizer* doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
//单手指双击
[doubleTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleTap];
//双手指双击
UITapGestureRecognizer * twoFingerDoubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerDoubleTap:)];
[twoFingerDoubleTap setNumberOfTapsRequired:2];
//两个手指 默认为一个
[twoFingerDoubleTap setNumberOfTouchesRequired:2];
[self.view addGestureRecognizer:twoFingerDoubleTap];
-(void)pressLeftBtn
{
//self.mm_drawerController通过遍历循环拿到之前在AppDelegate中声明的那个MMDrawerController属性,然后判断是否为打开状态,如果是就关闭,否就是打开
[self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
-(void)pressRightBtn
{
[self.mm_drawerController toggleDrawerSide:MMDrawerSideRight 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];
}
UITableViewDataSource
、UITableViewDelegate
的相关协议函数ShowViewControll
视图控制器中-(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];
}];
}
需要注意的是,push成功之后,必须要关闭掉我们的抽屉效果。否则,显示ShowViewController的时候仍然是抽屉的样式
在该视图控制器中只设置了背景色为白色。
选中cell push进入ShowViewController之后,当你轻轻往右滑动的时候 其实它已经加载了centerViewController中的viewWillAppear这个方法,这个方法打开了通过手势滑动打开抽屉的模式 ,因此导致没有回到centerViewController,而显示的是抽屉模式。你在ShowViewController的viewWillAppear这个方法中加上关闭通过手势滑动打开抽屉的模式就可以了
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//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 = 300.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;
}
总体上大致流程就是如此了!