控制器抽屉侧滑,左中右三个控制器,这里使用上面提到的MMDrawerController框架。
1、首先创建三个控制器为center、left、right,创建完成之后,我们来到我们的AppDelegate,开始编写我们的代码了
1.1多话不说,先导入头文件,并且添加一个MMDrawerController的属性
//为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
1.2 上面的做完后,我们便要显示我们的窗口到设备上,接下来来到这里
//1、初始化控制器
UIViewController*leftVC = [[leftViewControlleralloc]init];
UIViewController*rightVC = [[rightViewControlleralloc]init];
//2、初始化导航控制器
UINavigationController *leftNvaVC = [[UINavigationController alloc]initWithRootViewController:leftVC];
UINavigationController *rightNvaVC = [[UINavigationController alloc]initWithRootViewController:rightVC];
//3、使用MMDrawerController
self.drawerController = [[MMDrawerController alloc]initWithCenterViewController:leftNvaVC leftDrawerViewController:rightNvaVC ];
//4、设置打开/关闭抽屉的手势
self.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
self.drawerController.closeDrawerGestureModeMask =MMCloseDrawerGestureModeAll;
//5、设置左右两边抽屉显示的多少
self.drawerController.maximumLeftDrawerWidth = 200.0;
self.drawerController.maximumRightDrawerWidth = 200.0;
ScrollViewController *scroll = [[ScrollViewController alloc]init];
self.window.rootViewController = scroll;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(8.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//6、初始化窗口、设置根控制器、显示窗口
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window setRootViewController:self.drawerController];
[self.window makeKeyAndVisible];
});
2、完成上面后基本的抽屉效果就已经实现了,在这里的话,我们将要实现导航栏上面的按钮,以及一些效果。
2.1完成上面后,导航栏的点击就能切换,那么我们就来实现一个效果吧(ViewController),所谓的弹簧效果,也就几句代码
self.title = @"侧滑";
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];
2.2这里的话就是我们的方法,其实很简单
-(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.在侧滑页面做一个表格,每个cell都是能跳转一个界面
- (void)viewDidLoad {
[super viewDidLoad];
self.title=@"侧滑页面";
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
tableView.delegate=self;
tableView.dataSource=self;
[self.viewaddSubview:tableView];
arr = @[@"1",@"2",@"3",@"4",@"5"];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return 5;
}
-(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=arr[indexPath.row];
returncell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
ViewController *vc = [[ViewController alloc]init];
vc.title=arr[indexPath.row];
//拿到我们的LitterLCenterViewController,让它去push
UINavigationController* nav = (UINavigationController*)self.mm_drawerController.centerViewController;
[navpushViewController:vc animated:NO];
//当我们push成功之后,关闭我们的抽屉
[self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
//设置打开抽屉模式为MMOpenDrawerGestureModeNone,也就是没有任何效果。
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
}];
}