IOS中MMDrawerController第三方抽屜效果的基本使用示例
因?yàn)閯傞_年,所以最近公司比較閑,看到以前并不是我接手的項(xiàng)目中有這種抽屜效果的控制器,比較感興趣,便對(duì)MMDrawerController研究起來。也方便自己忘記之后查閱,另外也希望對(duì)大家有所幫助(PS:以前都是上面一個(gè)導(dǎo)航欄,下面一個(gè)tabbar的項(xiàng)目居多,所以對(duì)這種抽屜控制器不是很了解).
1.首先,到GitHub上把MMDrawerController下下來,然后倒入到項(xiàng)目中。當(dāng)然你用cocoapods倒入也行??茨阈那閱hO(∩_∩)O
2.接下來就在appdelegate中擼我們的代碼了。先倒入各個(gè)控制器哈。
#import"MMDrawerController.h" #import"rightViewController.h" #import"centerViewController.h" #import"leftViewController.h" #import"MainNavViewController.h"
然后就是在didFinishLaunching中設(shè)置相關(guān)的控制了,其實(shí)跟平時(shí)項(xiàng)目的區(qū)別就是多了一個(gè)抽屜控制器。
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { self.window= [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds]; //左中右三個(gè)控制器 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)閉模式手勢(shì) self.mmDrawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll; // 打開模式手勢(shì) self.mmDrawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll; // 抽屜控制器的最長(zhǎng)寬度 self.mmDrawerController.maximumLeftDrawerWidth = 200; [self.windowmakeKeyAndVisible]; self.window.rootViewController=self.mmDrawerController; returnYES; }
其實(shí)在這里就已經(jīng)可以實(shí)現(xiàn)抽屜控制器的基本效果的了。但是要如下圖的效果還得加一丟丟代碼。
然后我們?cè)赾enter控制器導(dǎo)航欄的leftBarButton上自定義一個(gè)button,添加點(diǎn)擊事件等等,這應(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上添加了一個(gè)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; }
點(diǎn)擊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中打開滑動(dòng)的手勢(shì)
-(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll]; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS 使用 socket 實(shí)現(xiàn)即時(shí)通信示例(非第三方庫(kù))
這篇文章主要介紹了iOS 使用 socket 即時(shí)通信示例(非第三方庫(kù))的資料,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-02-02IOS 中UITextField,UITextView,UILabel 根據(jù)內(nèi)容來計(jì)算高度
這篇文章主要介紹了IOS 中UITextField,UITextView,UILabel 根據(jù)內(nèi)容來計(jì)算高度的相關(guān)資料,需要的朋友可以參考下2017-03-03iOS毛玻璃效果的實(shí)現(xiàn)及圖片模糊效果的三種方法
App設(shè)計(jì)時(shí)往往會(huì)用到一些模糊效果或者毛玻璃效果,iOS目前已提供一些模糊API可以讓我們方便是使用,本文給大家介紹iOS毛玻璃效果的實(shí)現(xiàn)及圖片模糊效果的三種方法,感興趣的朋友一起學(xué)習(xí)吧2016-01-01iOS中實(shí)現(xiàn)動(dòng)態(tài)區(qū)域裁剪圖片功能實(shí)例
圖片處理中經(jīng)常用的圖片剪裁,就是通過剪裁框確定圖片剪裁的區(qū)域,然后剪去該區(qū)域的圖片,下面這篇文章主要給大家介紹了關(guān)于iOS中實(shí)現(xiàn)動(dòng)態(tài)區(qū)域裁剪圖片功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起看看吧。2017-11-11IOS 開發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS 開發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(二)
這篇文章主要介紹了IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能,點(diǎn)擊獲取驗(yàn)證碼,進(jìn)入時(shí)間倒計(jì)時(shí),感興趣的小伙伴們可以參考一下2016-04-04iOS實(shí)現(xiàn)兩個(gè)控制器之間數(shù)據(jù)的雙向傳遞
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)兩個(gè)控制器之間數(shù)據(jù)的雙向傳遞的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05