最近开发中的一个需求是将以前UITabBarController管理的页面结构,改成抽屉效果,所以我就使用了MMDrawerController这个开源框架,github地址:https://github.com/mutualmobile/MMDrawerController
。
MMDrawerController具体的用法这里就不详细说了,大家可以参考 这篇文章-iOS-MMDrawerController的使用。我们现在的需求是左侧抽屉打开后点击上面的按钮push出下一个页面,当这个push出来的页面pop回来以后左侧抽屉仍然保持打开的状态。
上图这样显然不是想要的效果。push出的页面应该从屏幕的边缘出现才对嘛!就像下图这样:
想要的效果.gif我想到的思路是当点击PUSH按钮的时候,将当前屏幕截图,然后添加到左边抽屉的View上,同时调整抽屉的
maximumLeftDrawerWidth
为屏幕的宽度并取消阴影。在左侧控制器的- (void)viewDidAppear:(BOOL)animated
方法中移除截屏图片,并且将maximumLeftDrawerWidth
恢复为初始宽度并恢复显示阴影,这样pop回来以后页面看起来就跟push前一样了。
/// 添加当前页面的截屏
- (void)addCurrentPageScreenshot {
UIImage *screenImage = [UIImage screenshot];
UIImageView *imgView = [[UIImageView alloc] initWithImage:screenImage];
imgView.image = screenImage;
[self.view addSubview:imgView];
self.coverImageView = imgView;
}
/// 设置抽屉视图pop后的状态
- (void)settingDrawerWhenPop {
self.mm_drawerController.maximumLeftDrawerWidth = [UIScreen mainScreen].bounds.size.width - 50;
self.mm_drawerController.showsShadow = YES;
self.mm_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
[self.coverImageView removeFromSuperview];
self.coverImageView = nil;
}
/// 设置抽屉视图push后的状态
- (void)settingDrawerWhenPush {
[self.mm_drawerController setMaximumLeftDrawerWidth:[UIScreen mainScreen].bounds.size.width];
self.mm_drawerController.showsShadow = NO;
// 这里一定要关闭手势,否则隐藏在屏幕右侧的drawer可以被拖拽出来
self.mm_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModeNone;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self settingDrawerWhenPop];
}
- (void)gotoSetting:(UIButton *)sender {
[self addCurrentPageScreenshot];
[self settingDrawerWhenPush];
SettingViewController *settingVc = [[SettingViewController alloc] init];
[self.navigationController pushViewController:settingVc animated:YES];
}
**-- 顺便贴出截屏的方法 -- **
#import "UIImage+Extension.h"
@implementation UIImage (Extension)
/// 截屏
+ (UIImage *)screenshot {
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, [window center].x, [window center].y);
CGContextConcatCTM(context, [window transform]);
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y);
[[window layer] renderInContext:context];
CGContextRestoreGState(context);
}
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
如果各位有其他方法或者思路,一起讨论一下吧!
Demo代码在这里https://github.com/yzxcool123/MMDrawerController-Demo.git
网友评论
- (void)setViewController:(UIViewController *)viewController{
[self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
[self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeNone];
}];
viewController.hidesBottomBarWhenPushed = YES;
UITabBarController * nav = (UITabBarController*)self.mm_drawerController.centerViewController;
UINavigationController *vc = nav.viewControllers[nav.selectedIndex];
[vc pushViewController:viewController animated:YES];
}
我现在是这样进行跳转的