最近開發(fā)中的一個需求是將以前UITabBarController管理的頁面結(jié)構(gòu)哀墓,改成抽屜效果,所以我就使用了MMDrawerController這個開源框架激况,github地址:https://github.com/mutualmobile/MMDrawerController
投储。
MMDrawerController具體的用法這里就不詳細(xì)說了拱她,大家可以參考 這篇文章-iOS-MMDrawerController的使用炸茧。我們現(xiàn)在的需求是左側(cè)抽屜打開后點擊上面的按鈕push出下一個頁面瑞妇,當(dāng)這個push出來的頁面pop回來以后左側(cè)抽屜仍然保持打開的狀態(tài)。
上圖這樣顯然不是想要的效果梭冠。push出的頁面應(yīng)該從屏幕的邊緣出現(xiàn)才對嘛辕狰!就像下圖這樣:
我想到的思路是當(dāng)點擊PUSH按鈕的時候,將當(dāng)前屏幕截圖控漠,然后添加到左邊抽屜的View上蔓倍,同時調(diào)整抽屜的
maximumLeftDrawerWidth
為屏幕的寬度并取消陰影。在左側(cè)控制器的- (void)viewDidAppear:(BOOL)animated
方法中移除截屏圖片,并且將maximumLeftDrawerWidth
恢復(fù)為初始寬度并恢復(fù)顯示陰影偶翅,這樣pop回來以后頁面看起來就跟push前一樣了他去。
/// 添加當(dāng)前頁面的截屏
- (void)addCurrentPageScreenshot {
UIImage *screenImage = [UIImage screenshot];
UIImageView *imgView = [[UIImageView alloc] initWithImage:screenImage];
imgView.image = screenImage;
[self.view addSubview:imgView];
self.coverImageView = imgView;
}
/// 設(shè)置抽屜視圖pop后的狀態(tài)
- (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;
}
/// 設(shè)置抽屜視圖push后的狀態(tài)
- (void)settingDrawerWhenPush {
[self.mm_drawerController setMaximumLeftDrawerWidth:[UIScreen mainScreen].bounds.size.width];
self.mm_drawerController.showsShadow = NO;
// 這里一定要關(guān)閉手勢,否則隱藏在屏幕右側(cè)的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