博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS之侧滑界面实现
阅读量:5138 次
发布时间:2019-06-13

本文共 3754 字,大约阅读时间需要 12 分钟。

#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
*)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

你可能感兴趣的文章
Who's in the Middle
查看>>
创建私有cocoapods
查看>>
Customers Who Never Order
查看>>
HR给应届生的黄金面试技巧
查看>>
model.js
查看>>
iOS开发支付集成之微信支付
查看>>
技术人员的发展之路
查看>>
redis缓存数据库
查看>>
Maven的安装及配置
查看>>
oracle中sum求和问题
查看>>
Nginx配置upstream实现负载均衡
查看>>
详解post和get请求
查看>>
16 合并两个排序的链表Merge two sorted linkedlist<TODO输入两个链表>
查看>>
Qt5学习笔记 | 给窗口添加动作
查看>>
Html Table to Excel 的一种实现 (PHP)
查看>>
将MSSQL2005的用户数据库导入到godaddy
查看>>
Fluent NHibernate Component
查看>>
poj 1190 DFS 不等式放缩进行剪枝
查看>>
我所理解的RESTful Web API [Web标准篇]
查看>>
RabbitMQ与.net core(一)安装
查看>>