說明:
最近在項目中需要實現(xiàn)一個抽屜效果,后來查了查資料覺得
MMDrawerController
還是比較不錯的,下載下來試了一試MMDrawerController順便做了一個小Demo,下圖(希望能幫助大家):
image
這里的話我是直接使用的
cocoapods
,我就不詳細(xì)介紹了,這里我就直接說Demo了,使用為純代碼
代碼:
1、首先創(chuàng)建三個控制器為center、left、right(我這里就簡寫了,具體的話大家可以下載Demo),創(chuàng)建完成之后,我們來到我們的AppDelegate,開始編寫我們的代碼了
- 1.1、多話不說,先導(dǎo)入頭文件,并且添加一個MMDrawerController的屬性
//為MMDrawerController框架中
#import "MMDrawerController.h"
#import "UIViewController+MMDrawerController.h"
//為自己創(chuàng)建的三個控制器
#import "LitterLCenterViewController.h"
#import "LitterLLeftViewController.h"
#import "LitterLRightViewController.h"
@interface LitterLAppDelegate ()
/**
* MMDrawerController屬性
*/
@property(nonatomic,strong) MMDrawerController * drawerController;
@end
- 1.2、上面的做完后,我們便要顯示我們的窗口到設(shè)備上,接下來來到這里
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1、初始化控制器
UIViewController *centerVC = [[LitterLCenterViewController alloc]init];
UIViewController *leftVC = [[LitterLLeftViewController alloc]init];
UIViewController *rightVC = [[LitterLRightViewController alloc]init];
//2、初始化導(dǎo)航控制器
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、設(shè)置打開/關(guān)閉抽屜的手勢
self.drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
self.drawerController.closeDrawerGestureModeMask =MMCloseDrawerGestureModeAll;
//5、設(shè)置左右兩邊抽屜顯示的多少
self.drawerController.maximumLeftDrawerWidth = 200.0;
self.drawerController.maximumRightDrawerWidth = 200.0;
//6、初始化窗口、設(shè)置根控制器、顯示窗口
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window setRootViewController:self.drawerController];
[self.window makeKeyAndVisible];
return YES;
}
2、完成上面后基本的抽屜效果就已經(jīng)實現(xiàn)了,在這里的話,我們將要實現(xiàn)導(dǎo)航欄上面的按鈕,以及一些效果。
- 2.1、這里的話我們先實現(xiàn)導(dǎo)航欄的效果吧:
- 2.1.1、這里的話我用的是通過UIBarButtonItem的方法去實現(xiàn)的
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)];
- 2.1.2、然而我們的作者給了我們一個類,里面有他通過QuartzCore繪制出來的按鈕,你們想看的話可以去看看
//首先得導(dǎo)入頭文件
#import "MMDrawerBarButtonItem.h"
//----------------------------
self.navigationItem.leftBarButtonItem = [[MMDrawerBarButtonItem alloc]initWithTarget:self action:@selector(leftBtn)];
self.navigationItem.rightBarButtonItem = [[MMDrawerBarButtonItem alloc]initWithTarget:self action:@selector(rightBtn)];
- 2.1.3、這里的話就是我們的方法,其實很簡單(就一行代碼)但是過程很迷茫
//首先得導(dǎo)入頭文件
#import "UIViewController+MMDrawerController.h"
//----------------------------
-(void)leftBtn{
//這里的話是通過遍歷循環(huán)拿到之前在AppDelegate中聲明的那個MMDrawerController屬性,然后判斷是否為打開狀態(tài),如果是就關(guān)閉,否就是打開(初略解釋,里面還有一些條件)
[self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
-(void)rightBtn{
[self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
- 2.2、完成上面后,導(dǎo)航欄的點擊就能切換,那么我們就來實現(xiàn)一個效果吧,所謂的彈簧效果,也就幾句代碼
//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、兩個手指 默認(rèn)為一個
[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、到這里的話就是最后一步了,第一設(shè)置數(shù)據(jù)源,第二實現(xiàn)協(xié)議了。
- 3.1、數(shù)據(jù)源的編寫,這里的話我用的都是靜態(tài)數(shù)據(jù),就不做解釋了,右側(cè)和左側(cè)抽屜都為一樣的,你們自行查看吧
#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;
}
- 3.2、這里就是我們的最后一步,點擊Cell跳轉(zhuǎn)控制器了,那么我們的有一個控制器取名去:
LitterLShowViewController
#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];
//當(dāng)我們push成功之后,關(guān)閉我們的抽屜
[self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
//設(shè)置打開抽屜模式為MMOpenDrawerGestureModeNone,也就是沒有任何效果。
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
}];
}
4、差點忘了一個,就是當(dāng)我們的LitterLShowViewController退出后,我們的把打開抽屜模式在切換過來,當(dāng)然這個是在中間控制器里面去寫,因為LitterLShowViewController退出后會呈現(xiàn)中間控制器
/**
* 加載控制器的時候設(shè)置打開抽屜模式 (因為在后面會關(guān)閉)
*/
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//設(shè)置打開抽屜模式
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
}
Demo地址:MMDrawerControllerDemo
本章到此結(jié)束
歡迎各位碼友隨意轉(zhuǎn)載并指正