您的位置:首页 > 编程语言

MMDrawerController,最简代码实现抽屉效果

2016-05-05 19:30 218 查看
pod'MMDrawerController','~> 0.5.7'

将MMDrawerController集成到我们的工程

建议在APPdelegate内完成MMDrawerController的基本配置

#import <MMDrawerController.h>

@property (nonatomic,strong)MMDrawerController * drawerController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//添加左视图
LeftVC *left = [[LeftVC alloc] init];
UINavigationController *LeftNav = [[UINavigationController alloc] initWithRootViewController:left];

//添加中视图
CenterVC *center = [[CenterVC alloc] init];
UINavigationController *CenterNav = [[UINavigationController alloc] initWithRootViewController:center];

//添加右视图
RightVC *right = [[RightVC alloc] init];
UINavigationController *RightNav = [[UINavigationController alloc] initWithRootViewController:right];

//添加主控制器
self.drawerController = [[MMDrawerController alloc] initWithCenterViewController:CenterNav leftDrawerViewController:LeftNav rightDrawerViewController:RightNav];

//简单配置
[self.drawerController setRestorationIdentifier:@"MMDrawer"];
// [self.drawerController setMaximumRightDrawerWidth:200];
[self.drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
[self.drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];

//一定要记得指定根视图
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window setTintColor:[UIColor redColor]];
[self.window setRootViewController:self.drawerController];

return YES;
}

========
点击左右切换视图
@property(nonatomic,strong, readonly)MMDrawerController *mm_drawerController;

-(MMDrawerController*)mm_drawerController{
UIViewController *parentViewController = self.parentViewController;
while (parentViewController != nil) {
if([parentViewController isKindOfClass:[MMDrawerController class]]){
return (MMDrawerController *)parentViewController;
}
parentViewController = parentViewController.parentViewController;
}
return nil;
}

- (IBAction)left:(id)sender {
[self.mm_drawerControllertoggleDrawerSide:MMDrawerSideLeftanimated:YEScompletion:nil];
}
- (IBAction)right:(id)sender {
[self.mm_drawerControllertoggleDrawerSide:MMDrawerSideRightanimated:YEScompletion:nil];
}

如果想只在当前这个VC内可以侧滑

那么要设置

#pragma -mark 防止下一页面也会滑动
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];

[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
[self.mm_drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeNone];

}

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {

}];
self.mm_drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
self.mm_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: