IOS中MMDrawerController第三方抽屉效果的基本使用示例
程序员文章站
2023-12-20 20:32:46
因为刚开年,所以最近公司比较闲,看到以前并不是我接手的项目中有这种抽屉效果的控制器,比较感兴趣,便对mmdrawercontroller研究起来。也方便自己忘记之后查阅,另...
因为刚开年,所以最近公司比较闲,看到以前并不是我接手的项目中有这种抽屉效果的控制器,比较感兴趣,便对mmdrawercontroller研究起来。也方便自己忘记之后查阅,另外也希望对大家有所帮助(ps:以前都是上面一个导航栏,下面一个tabbar的项目居多,所以对这种抽屉控制器不是很了解).
1.首先,到github上把mmdrawercontroller下下来,然后倒入到项目中。当然你用cocoapods倒入也行。看你心情呗o(∩_∩)o
2.接下来就在appdelegate中撸我们的代码了。先倒入各个控制器哈。
#import"mmdrawercontroller.h" #import"rightviewcontroller.h" #import"centerviewcontroller.h" #import"leftviewcontroller.h" #import"mainnavviewcontroller.h"
然后就是在didfinishlaunching中设置相关的控制了,其实跟平时项目的区别就是多了一个抽屉控制器。
- (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]; //导航控制器 mainnavviewcontroller*rightnavvc = [[mainnavviewcontrolleralloc]initwithrootviewcontroller:rightvc]; mainnavviewcontroller*leftnavvc = [[mainnavviewcontrolleralloc]initwithrootviewcontroller:leftvc]; mainnavviewcontroller*centernavvc = [[mainnavviewcontrolleralloc]initwithrootviewcontroller:centervc]; //抽屉控制器 self.mmdrawercontroller= [[mmdrawercontrolleralloc]initwithcenterviewcontroller:centernavvcleftdrawerviewcontroller:leftnavvcrightdrawerviewcontroller:rightnavvc]; // 关闭模式手势 self.mmdrawercontroller.closedrawergesturemodemask = mmclosedrawergesturemodeall; // 打开模式手势 self.mmdrawercontroller.opendrawergesturemodemask = mmopendrawergesturemodeall; // 抽屉控制器的最长宽度 self.mmdrawercontroller.maximumleftdrawerwidth = 200; [self.windowmakekeyandvisible]; self.window.rootviewcontroller=self.mmdrawercontroller; returnyes; }
其实在这里就已经可以实现抽屉控制器的基本效果的了。但是要如下图的效果还得加一丢丢代码。
然后我们在center控制器导航栏的leftbarbutton上自定义一个button,添加点击事件等等,这应该不难哈。记得要导入相关的类。
#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跳转控制器
-(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]; //关闭了控制器之后记得将模式设置为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]; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。