IOS抽屜效果的實現

weixin_33866037發表於2016-10-07

抽屜檢視實現的思路

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];
    

    
}




相關文章