iOS开发之如何实现“抽屉”效果
现在基本上每一个App中左划都会出现一个页面,基本上都是只占主页面的一部分,效果就像是一个抽屉一样。最近在写项目时,关于如何达到抽屉效果,总结了一些东西。
先看看效果图:
实现过程
首先我们需要去创建一个新的视图控制器,让它作为我们的要实现的抽屉的根视图,在此视图控制器我们要添加对应的左视图,要是需要右视图也可以添加,然后设定方法:
@property (nonatomic, strong) UIViewController *rootViewController;
//左侧视图
@property (nonatomic, strong) UIViewController *leftViewController;
//菜单宽度
@property (nonatomic, assign, readonly) CGFloat menuWidth;
//留白宽度
@property (nonatomic, assign, readonly) CGFloat emptyWidth;
//是否允许滚动
@property (nonatomic ,assign) BOOL slideEnabled;
//创建方法
-(instancetype)initWithRootViewController:(UIViewController*)rootViewController;
//显示主视图
-(void)showRootViewControllerAnimated:(BOOL)animated;
//显示左侧菜单
-(void)showLeftViewControllerAnimated:(BOOL)animated;
接着我们将定义的方法进行实现:
-(instancetype)initWithRootViewController:(UIViewController*)rootViewController{
if (self = [super init]) {
_rootViewController = rootViewController;
[self addChildViewController:_rootViewController];
[self.view addSubview:_rootViewController.view];
[_rootViewController didMoveToParentViewController