左中右抽屉

左右抽屉

介绍:左中右为三个自定义的控制器
DraWerViewController
DraWerViewController.h
- (instancetype)initWithLeftVC:(UIViewController *)leftvc
                        mainvc:(UITabBarController *)mainvc
                         right:(UIViewController *)rightvc;
-(void)openLeftDrawer;
-(void)closeLeftDrawer;
-(void)openRightDrawer;
-(void)closeRightDrawer;
-(void)logout;
DraWerViewController.m
//登陆页面
#import "LoginViewController.h"
//宏定义左右抽屉大小
#define Kleftview KScreen_W * (5.0/6)
#define Krightview KScreen_W * (5/6.0)
@interface DrawerViewController ()
@property (nonatomic,strong)UIViewController *leftViewController;
@property (nonatomic,strong)UIViewController *rightViewController;
@property (nonatomic,strong)UITabBarController *mainViewController;
@property (nonatomic,strong)UIButton *maskBtn;
-(void)initializeDataSource;/**< 初始化数据源 */
-(void)initializeUserInterface;/**< 初始化用户界面 */
@end

@implementation DrawerViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    [self toLogin];
    [self initializeDataSource];
    [self initializeUserInterface];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- 判断如何登录
-(void)toLogin{
    //本地化一个标示
    //获取本地存储的用户名和密码,不为空则自动登录
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isAutoLogin"]) {
        // 自动登录
        [self autoLogin];
    }else{
    // 显示登录界面
        [self showLoginVC];
    }
}
    // 自动登录
-(void)autoLogin{
    // 做登录的网络请求
}
    // 输入登录,弹出登录框
-(void)showLoginVC{
    // 推出登录界面
    UINavigationController *loginNav = [[UINavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
    loginNav.navigationBar.hidden = YES;
    [self presentViewController:loginNav animated:NO completion:nil];
    // 关闭抽屉
    [self closeLeftDrawer];
    // 跳转到首页界面
    self.mainViewController.selectedIndex = 0 ;
}

-(void)initializeDataSource/**< 初始化数据源 */{

}

-(void)initializeUserInterface/**< 初始化用户界面 */{
    if (self.leftViewController) {
        //添加子控制器,添加界面
        [self addChildViewController:self.leftViewController];
        self.leftViewController.view.frame = CGRectMake(-Kleftview, 0, KScreen_W, KScreen_H);
    }
    if (self.mainViewController){
        [self addChildViewController:self.mainViewController];
        self.mainViewController.view.frame = CGRectMake(0, 0, KScreen_W, KScreen_H);
    }
    if(self.rightViewController){
        [self addChildViewController:self.rightViewController];
        self.rightViewController.view.frame = CGRectMake(KScreen_W, 0, KScreen_W, KScreen_H);
    }
}
#pragma mark -- Public Method
-(void)logout{
    [self showLoginVC];
    // 修改自动登录状态
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isAutoLogin"];
}


#pragma mark -- 重写初始化方法
- (instancetype)initWithLeftVC:(UIViewController *)leftvc
                        mainvc:(UITabBarController *)mainvc
                         right:(UIViewController *)rightvc
{
    self = [super init];
    if (self) {
        _leftViewController = leftvc;
        _mainViewController = mainvc;
        _rightViewController = rightvc;
    }
    return self;
}

#pragma mark -- 方法重写
    //重写方法
-(void)addChildViewController:(UIViewController *)childController{
    [super addChildViewController:childController];
    [self.view addSubview:childController.view];
    [childController didMoveToParentViewController:self];

}

#pragma mark -- 点击方法实现部分
-(void)action_buttonPressed{
    [self closeRightDrawer];
    [self closeLeftDrawer];
}

-(void)openLeftDrawer{
    [self.mainViewController.view addSubview:self.maskBtn];
    self.maskBtn.alpha = 1;
    [UIView animateWithDuration:0.8 animations:^{
        self.leftViewController.view.frame = CGRectMake(0, 0, Kleftview, KScreen_H);
        self.mainViewController.view.frame = CGRectMake(Kleftview, 0, KScreen_W, KScreen_H);
    }];
}

-(void)closeLeftDrawer{
    self.maskBtn.alpha = 0;
    [UIView animateWithDuration:0.8 animations:^{
        self.leftViewController.view.frame = CGRectMake(-Kleftview, 0, Kleftview, KScreen_H);
        self.mainViewController.view.frame = CGRectMake(0, 0, KScreen_W, KScreen_H);
    }];
}

-(void)openRightDrawer{
    [self.mainViewController.view addSubview:self.maskBtn];
    self.maskBtn.alpha = 1;
    [UIView animateWithDuration:0.8 animations:^{
        self.rightViewController.view.frame = CGRectMake(KScreen_W*(1/6.0), 0, Krightview, KScreen_H);
        self.mainViewController.view.frame = CGRectMake(-Krightview, 0, KScreen_W, KScreen_H);
    }];
}

-(void)closeRightDrawer{
    self.maskBtn.alpha = 0;
    [UIView animateWithDuration:0.8 animations:^{
        self.rightViewController.view.frame = CGRectMake(KScreen_W, 0, Krightview, KScreen_H);
        self.mainViewController.view.frame = CGRectMake(0, 0, KScreen_W, KScreen_H);
    }];
}
#pragma mark -- 懒加载
-(UIButton *)maskBtn{
    if (!_maskBtn) {
        _maskBtn = [[UIButton alloc]init];
        _maskBtn.frame = CGRectMake(0, 64, KScreen_W, KScreen_H-64);
        _maskBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [_maskBtn addTarget:self action:@selector(action_buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    }
    return _maskBtn;
}

你可能感兴趣的:(ios开发)