1径密、首先創(chuàng)建三個控制器為center回梧、left盟广、right(我這里就簡寫了,具體的話大家可以下載Demo)闷串,創(chuàng)建完成之后,我們來到我們的AppDelegate筋量,開始編寫我們的代碼了
這個的demo???? https://github.com/LitterL/MMDrawerControllerDemo
?? 這個是沒有跳多的 ???? https://github.com/manguoge/Custom_ViewDeck
1.1多話不說烹吵,先導入頭文件,并且添加一個MMDrawerController的屬性
都在appdelegate.m里
//為MMDrawerController框架中
#import"MMDrawerController.h"
#import"UIViewController+MMDrawerController.h"
//為自己創(chuàng)建的三個控制器
#import"LitterLCenterViewController.h"
#import"LitterLLeftViewController.h"
#import"LitterLRightViewController.h"
@interface LitterLAppDelegate ()
? //記得寫這個玩意
@property(nonatomic,strong) MMDrawerController * drawerController;
? @end
1.2?上面的做完后毛甲,我們便要顯示我們的窗口到設備上年叮,接下來來到這里
(還是在appDelegate里寫的)
- (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;
}
//上面是AppDelegate.m里的
//下面是Center里的
2爆存、完成上面后基本的抽屜效果就已經(jīng)實現(xiàn)了蛉顽,在這里的話,我們將要實現(xiàn)導航欄上面的按鈕先较,以及一些效果携冤。
2.1、這里的話我們先實現(xiàn)導航欄的效果吧:
2.1.1闲勺、這里的話我用的是通過UIBarButtonItem的方法去實現(xiàn)的
//設置導航欄左右按鈕
//左
? ? self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(leftBtn)];
? ? //右
? ? self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"右" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtn)];
2.1.2曾棕、然而我們的作者給了我們一個類,里面有他通過QuartzCore繪制出來的按鈕菜循,你們想看的話可以去看看
//沒什么用不寫也行的
//首先得導入頭文件
#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翘地、這里的話就是我們的方法,其實很簡單(就一行代碼)但是過程很迷茫
//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];
//首先得導入頭文件
#import"UIViewController+MMDrawerController.h"
//----------------------------
-(void)leftBtn{
//這里的話是通過遍歷循環(huán)拿到之前在AppDelegate中聲明的那個MMDrawerController屬性,然后判斷是否為打開狀態(tài)胶逢,如果是就關閉渴杆,否就是打開(初略解釋,里面還有一些條件)?
[self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
-(void)rightBtn{
? [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
2.2宪塔、完成上面后磁奖,導航欄的點擊就能切換,那么我們就來實現(xiàn)一個效果吧某筐,所謂的彈簧效果比搭,也就幾句代碼
? //----------------------------/**
? *? 添加點擊手勢? 一個手指雙擊
? */
-(void)doubleTap:(UITapGestureRecognizer*)gesture{
? ? ? [self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideLeft completion:nil];
? }
? /**
? *? 添加點擊手勢? 兩個個手指雙擊
? */
-(void)twoFingerDoubleTap:(UITapGestureRecognizer*)gesture{
? ? ? [self.mm_drawerController bouncePreviewForDrawerSide:MMDrawerSideRight completion:nil];
? }
3、到這里的話就是最后一步了南誊,第一設置數(shù)據(jù)源身诺,第二實現(xiàn)協(xié)議了。
3.1抄囚、數(shù)據(jù)源的編寫霉赡,這里的話我用的都是靜態(tài)數(shù)據(jù),就不做解釋了幔托,右側和左側抽屜都為一樣的穴亏,你們自行查看吧
#pragmamark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? return10;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
? ? ? ? staticNSString *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跳轉控制器了,那么我們的有一個控制器取名去:LitterLShowViewController
#pragmamark - UITableViewDelegate-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
? ? LitterLShowViewController *showVC = [[LitterLShowViewController alloc]init];
? ? showVC.title = [NSString stringWithFormat:@"Left-Demo%ld",indexPath.row];
? ? //拿到我們的LitterLCenterViewController嗓化,讓它去pushUINavigationController* 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退出后會呈現(xiàn)中間控制器
/**
*? 加載控制器的時候設置打開抽屜模式? (因為在后面會關閉)
*/-(void)viewWillAppear:(BOOL)animated{
? ? [super viewWillAppear:animated];
? ? //設置打開抽屜模式? ? [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
}