iOS之侧滑界面实现

#import "CXDSideMenuController.h"
#import "AllControllerDispatchTool.h"
#import "SideHeaderView.h"
#import "Masonry.h"
#import "LoginViewController.h"

@interface CXDSideMenuController ()

//背景遮罩
@property (strong, nonatomic) UIView *backgroundImageView;

//内容视图
@property (strong, nonatomic) UIView *contentView;

//进入视图的序号
@property (assign, nonatomic) NSUInteger selectIndex;

//侧边栏headerView
@property (strong, nonatomic) SideHeaderView *headerView;

@end

@implementation CXDSideMenuController

#pragma mark - 初始化方法
+ (instancetype)shareSideMenu
{
    static CXDSideMenuController *sideMenuVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sideMenuVC = [self new];
    });
    return sideMenuVC;
    
}

//唤醒侧边菜单方法
+ (void)openSideMenuFromWindow:(UIWindow *)window
{
    //每次调用都能够保证唤醒的侧边菜单是唯一的一个
    CXDSideMenuController *sideMenu = [CXDSideMenuController shareSideMenu];
    [window addSubview:sideMenu.view];
    
    //唤醒之后,需要让侧边菜单移动进入当前视图
    [UIView animateWithDuration:0.5 animations:^{
        CGRect rect = sideMenu.contentView.frame;
        rect.origin.x = 0;
        sideMenu.contentView.frame = rect;
        sideMenu.backgroundImageView.alpha = 0.5;
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //侧滑菜单收回
    [self closeSideMenu];
    
}


//侧滑菜单收回(单独抽出,方便点击tableView的row发生对应响应)
- (void)closeSideMenu
{
    //如何拿到已经被唤醒的侧滑菜单
    [UIView animateWithDuration:0.5 animations:^{
        //侧滑菜单收回
        CGRect rect = self.contentView.frame;
        rect.origin.x = -rect.size.width;
        self.contentView.frame = rect;
        self.backgroundImageView.alpha = 0;
    } completion:^(BOOL finished) {
        //将侧滑菜单从当前视图移除
        [self.view removeFromSuperview];
        //进入相应的选择界面
        [AllControllerDispatchTool createViewControllerWithIndex:_selectIndex];
    }];
}

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];

    //构建界面
    [self.view addSubview:self.backgroundImageView];
    [self.view addSubview:self.contentView];


    [self.contentView addSubview:self.headerView];
    
    //添加约束
    //因为_headerView是加载在contentView之上,所以headerView得约束是根据contentView来计算的,因此在block当中,weakSelf必须为contentView类型
    __weak typeof(self.contentView)weakSelf = self.contentView;
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left);
        make.right.equalTo(weakSelf.mas_right);
        make.top.equalTo(weakSelf.mas_top);
        make.height.equalTo(110);
    }];


}

#pragma mark - 懒加载

-(UIView *)backgroundImageView
{
    if (!_backgroundImageView) {
        _backgroundImageView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundImageView.backgroundColor = [UIColor blackColor];
        _backgroundImageView.alpha = 0;
    }
    return _backgroundImageView;
}

-(UIView *)contentView
{
    if (!_contentView) {
        CGRect rect = [UIScreen mainScreen].bounds;
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(-rect.size.width*0.8, 0, rect.size.width*0.8, rect.size.height)];
        _contentView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
    }
    return _contentView;
}

-(SideHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [[SideHeaderView alloc] init];
        [_headerView.headImageBtn addTarget:self action:@selector(moveToLoginView) forControlEvents:UIControlEventTouchUpInside];
        [_headerView.userNameBtn addTarget:self action:@selector(moveToLoginView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headerView;
}

- (void)moveToLoginView
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self presentViewController:loginVC animated:YES completion:nil];
}

PS:通过按钮唤起侧滑菜单过程

PS:部分代码,仅供参考

转载于:https://www.cnblogs.com/chixuedong/p/5240015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值