#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:部分代码,仅供参考