抽屜視圖實現(xiàn)的思路
UIViewController 控制著一個 左邊的抽屜視圖(LeftViewController)
同時控制著一個UITabBarController.然后tabBarController就按我們平時做項目那樣做一個三級控制器披泪。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//創(chuàng)建窗口
_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;
}
//這里指的是上面那個控制所有控制器的根控制器(我創(chuàng)建的是叫做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>條件成立這個臨界值就讓當前的偏移量 = 這個臨界值
* ②再次判斷手勢的狀態(tài)是否結束,并且手指的偏移量是否小于這個臨界值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];
}