抽屉视图实现的思路
UIViewController 控制着一个 左边的抽屉视图(LeftViewController)
同时控制着一个UITabBarController.然后tabBarController就按我们平时做项目那样做一个三级控制器。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建窗口
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//设置背景颜色
_window.backgroundColor = [UIColor whiteColor];
//显示window
[_window makeKeyAndVisible];
BaseTabBarController *baseVC = [[BaseTabBarController alloc]init];
LeftViewController *leftVC = [[LeftViewController alloc]init];
RootViewController *rootVC = [[RootViewController alloc] initWithLeftViewController:leftVC withCenterViewController:baseVC];
_window.rootViewController = rootVC;
return YES;
}
//这里指的是上面那个控制所有控制器的根控制器(我创建的是叫做RootViewController的类)
RootViewController.h文件
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
- (instancetype)initWithLeftViewController:(UIViewController *)leftVC
withCenterViewController:(UIViewController *)centerVC;
- (void)closeLeftViewController:(UIViewController *)parentViewController;
@end
RootViewController.m文件
- (instancetype)initWithLeftViewController:(UIViewController *)leftVC
withCenterViewController:(UIViewController *)centerVC {
self = [super init];
if (self != nil) {
//添加子视图和子控制器
[self addChildViewController:leftVC];
leftVC.view.frame = CGRectMake(-200, 0, 200, self.view.bounds.size.height);
[self.view addSubview:leftVC.view];
[self addChildViewController:centerVC];
[self.view addSubview:centerVC.view];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.view addGestureRecognizer:pan];
}
return self;
}
#pragma mark - 手势
- (void)panAction:(UIPanGestureRecognizer *)pan {
/*
* 1.让抽屉视图的平移量跟随着手指的x轴偏移量相同
* 2.判断条件:
* ①是否在滑动的过程中大于等于(>=)某个临界值,这里我的临界值是200(左边抽屉视图的宽度)
* 1>条件成立这个临界值就让当前的偏移量 = 这个临界值
* ②再次判断手势的状态是否结束,并且手指的偏移量是否小于这个临界值200
* 1>条件成立就让当前手指的x轴偏移量为0
*
* 整个过程就是滑动量到达200时才显示抽屉视图没达到200就不显示
*/
CGPoint p = [pan translationInView:pan.view];
[UIView animateWithDuration:0.3 animations:^{
for (UIView *subView in self.view.subviews) {
CGPoint p1 = p;
if (p1.x >= 200) {
p1.x = 200;
}else if (pan.state == UIGestureRecognizerStateEnded && p1.x < 200){
p1.x = 0;
}
subView.transform = CGAffineTransformMakeTranslation(p1.x, 0);
}
}];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:0.3 animations:^{
for (UIView *subView in self.view.subviews) {
subView.transform = CGAffineTransformIdentity;
}
}];
}
#pragma mark - 关闭左边的抽屉
- (void)closeLeftViewController:(UIViewController *)parentViewController {
[UIView animateWithDuration:0.3 animations:^{
for (UIView *subView in parentViewController.view.subviews) {
subView.transform = CGAffineTransformIdentity;
}
}];
LeftViewController.m文件
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"点击" forState:UIControlStateNormal];
btn.frame = CGRectMake(0, 100, 70, 70);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(btnAc:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnAc:(UIButton *)btn {
RootViewController *root = [[RootViewController alloc]init];
TestViewController *test = [[TestViewController alloc]init];
//先关闭左边的抽屉视图
[root closeLeftViewController:self.parentViewController];
//要先pop到当前的根视图
[self.nv popToRootViewControllerAnimated:NO];
//push到你想要的控制器
[self.nv pushViewController:test animated:NO];
}
网友评论
```
if (p1.x <= 0) {
p1.x = 0;
}
```
这样就去掉了右滑的bug