实验原理:
MMDrawerController 是一个轻量级的易用的抽屉式导航控件。手指左右移动视图,主视图会移向旁边,在左边或者右边出现sidebar视图,一般是一些菜单。code4app有多份这种视图布局的代码,但这份代码能够对很多参数进行自定义,比如sidebar的宽度,是否需要阴影,是否支持pan手势、主视图移动的动画等等等等你能想到的属性都可以自定义。
使用方法:
创建3个控制器以及显示页面
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
MainViewController *centerVC = [[MainViewController alloc] init];
mainVC = [[MMDrawerController alloc] initWithCenterViewController:centerVC leftDrawerViewController:leftVC rightDrawerViewController:rightVC];
//1.设置右边控制器的显示宽度
[mainVC setMaximumRightDrawerWidth:200];
//2.设置左边控制器显示的宽度
[mainVC setMaximumLeftDrawerWidth:200];
//按钮滑动界面的时间参数
mainVC.animationVelocity = 180;
//3.设置打开、关闭的手势区域
[mainVC setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
[mainVC setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
//4.设置动画的类型(效果)
[[MMExampleDrawerVisualStateManager sharedManager] setLeftDrawerAnimationType:MMDrawerAnimationTypeParallax];
[[MMExampleDrawerVisualStateManager sharedManager] setRightDrawerAnimationType:MMDrawerAnimationTypeSwingingDoor];
[mainVC
setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
MMDrawerControllerDrawerVisualStateBlock block;
block = [[MMExampleDrawerVisualStateManager sharedManager]
drawerVisualStateBlockForDrawerSide:drawerSide];
if(block){
block(drawerController, drawerSide, percentVisible);
}
}];
//首次运行打开左边控制器:MMDrawerSideLeft
[mainVC openDrawerSide:MMDrawerSideLeft animated:YES completion:^(BOOL finished) {
NSLog(@"ss");
}];
//创建导航控制器
UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:mainVC];
//左边按钮的创建和实现方法
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.frame = CGRectMake(0, 0, 32, 32);
[leftButton setImage:[UIImage imageNamed:@"exchange_bg_home@2x.png"]
forState:UIControlStateNormal];
[leftButton addTarget:self action:@selector(actionbutton:)
forControlEvents:UIControlEventTouchUpInside];
MMDrawerBarButtonItem *leftItem = [[MMDrawerBarButtonItem alloc] initWithCustomView:leftButton];
[mainVC.navigationItem setLeftBarButtonItem:leftItem animated:YES];
//右边按钮的创建和实现方法
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 32, 32);
[rightButton setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home@2x.png"]
forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(actionbutton1:)
forControlEvents:UIControlEventTouchUpInside];
MMDrawerBarButtonItem *rightItem = [[MMDrawerBarButtonItem alloc] initWithCustomView:rightButton];
[mainVC.navigationItem setRightBarButtonItem:rightItem animated:YES];
mainVC.title = @"MMDrawer";
self.window.rootViewController = na;
//左边按钮方法,调用mmdramer中的方法
-(void)actionbutton:(id)sender{
[mainVC toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
//右边按钮方法,调用mmdramer中的方法
-(void)actionbutton1:(id)sender{
[mainVC toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}