IOS中MMDrawerController第三方抽屜效果的基本使用示例
因為剛開年,所以最近公司比較閑,看到以前并不是我接手的項目中有這種抽屜效果的控制器,比較感興趣,便對MMDrawerController研究起來。也方便自己忘記之后查閱,另外也希望對大家有所幫助(PS:以前都是上面一個導(dǎo)航欄,下面一個tabbar的項目居多,所以對這種抽屜控制器不是很了解).

1.首先,到GitHub上把MMDrawerController下下來,然后倒入到項目中。當(dāng)然你用cocoapods倒入也行。看你心情唄O(∩_∩)O

2.接下來就在appdelegate中擼我們的代碼了。先倒入各個控制器哈。
#import"MMDrawerController.h" #import"rightViewController.h" #import"centerViewController.h" #import"leftViewController.h" #import"MainNavViewController.h"
然后就是在didFinishLaunching中設(shè)置相關(guān)的控制了,其實跟平時項目的區(qū)別就是多了一個抽屜控制器。
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
self.window= [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
//左中右三個控制器
rightViewController*rightVc = [[rightViewControlleralloc]init];
leftViewController*leftVc = [[leftViewControlleralloc]init];
centerViewController*centerVc = [[centerViewControlleralloc]init];
//導(dǎo)航控制器
MainNavViewController*rightNavVc = [[MainNavViewControlleralloc]initWithRootViewController:rightVc];
MainNavViewController*leftNavVc = [[MainNavViewControlleralloc]initWithRootViewController:leftVc];
MainNavViewController*centerNavVc = [[MainNavViewControlleralloc]initWithRootViewController:centerVc];
//抽屜控制器
self.mmDrawerController= [[MMDrawerControlleralloc]initWithCenterViewController:centerNavVcleftDrawerViewController:leftNavVcrightDrawerViewController:rightNavVc];
// 關(guān)閉模式手勢
self.mmDrawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
// 打開模式手勢
self.mmDrawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
// 抽屜控制器的最長寬度
self.mmDrawerController.maximumLeftDrawerWidth = 200;
[self.windowmakeKeyAndVisible];
self.window.rootViewController=self.mmDrawerController;
returnYES;
}
其實在這里就已經(jīng)可以實現(xiàn)抽屜控制器的基本效果的了。但是要如下圖的效果還得加一丟丟代碼。

然后我們在center控制器導(dǎo)航欄的leftBarButton上自定義一個button,添加點擊事件等等,這應(yīng)該不難哈。記得要導(dǎo)入相關(guān)的類。
#import "UIViewController+MMDrawerController.h"
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"Demo";
self.view.backgroundColor = [UIColor greenColor];
//UIBarButtonItem的自定義的分類方法
self.navigationItem.leftBarButtonItem = [UIBarButtonItem initWithTarget:self action:@selector(leftBtnClick) image:@"菜單 (1)" hightImage:@"菜單"];
}
-(void)leftBtnClick{
// 將左邊的控制器打開
[self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
下面就是left控制器的代碼哈,就是在view上添加了一個tableView。
#import "leftViewController.h"
#import "pushViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "MainNavViewController.h"
@interface leftViewController ()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation leftViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blueColor];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.detailTextLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row];
return cell;
}
點擊cell跳轉(zhuǎn)控制器
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
pushViewController *pushVc = [[pushViewController alloc] init];
pushVc.title = [NSString stringWithFormat:@"%zd",indexPath.row];
//取到center控制器
MainNavViewController *mainNavVc = (MainNavViewController *)self.mm_drawerController.centerViewController;
[mainNavVc pushViewController:pushVc animated:YES];
//關(guān)閉了控制器之后記得將模式設(shè)置為None
[self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
}];
}
最后記得在center控制器的viewDidAppear中打開滑動的手勢
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS 使用 socket 實現(xiàn)即時通信示例(非第三方庫)
這篇文章主要介紹了iOS 使用 socket 即時通信示例(非第三方庫)的資料,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-02-02
IOS 中UITextField,UITextView,UILabel 根據(jù)內(nèi)容來計算高度
這篇文章主要介紹了IOS 中UITextField,UITextView,UILabel 根據(jù)內(nèi)容來計算高度的相關(guān)資料,需要的朋友可以參考下2017-03-03
iOS中實現(xiàn)動態(tài)區(qū)域裁剪圖片功能實例
圖片處理中經(jīng)常用的圖片剪裁,就是通過剪裁框確定圖片剪裁的區(qū)域,然后剪去該區(qū)域的圖片,下面這篇文章主要給大家介紹了關(guān)于iOS中實現(xiàn)動態(tài)區(qū)域裁剪圖片功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。2017-11-11
iOS實現(xiàn)兩個控制器之間數(shù)據(jù)的雙向傳遞
這篇文章主要為大家詳細(xì)介紹了iOS實現(xiàn)兩個控制器之間數(shù)據(jù)的雙向傳遞的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05

