标签:
#import <UIKit/UIKit.h> @interface MenuViewController : UIViewController ///左侧菜单视图 @property (nonatomic,strong) UIViewController *leftViewController; ///中间的视图 @property (nonatomic,strong) UIViewController *centerViewController; ///判断左侧菜单视图展开的状态 @property (nonatomic,assign,readonly) BOOL isShowing; ///菜单中的菜单项 @property (nonatomic,strong) NSMutableArray *menuArray; ///初始化方法 + (instancetype)instanceWithLeftViewController:(UIViewController *)leftVC CenterViewController:(UIViewController *)centerVC; ///获取抽屉方法 + (instancetype)getMenuViewController; ///展示左侧菜单的方法 - (void)showLeftViewController; ///关闭左侧菜单的方法 - (void)hideLeftViewController; @end |
#import "MenuViewController.h" ///抽屉展开的程度 #define kMenuLeftVCWidth [UIScreen mainScreen].bounds.size.width * 0.7 @interface MenuViewController ()<UITableViewDataSource,UITableViewDelegate> ///默认的自定义leftTableView @property (nonatomic,strong) UITableView *leftTableView; ///正在点击菜单项的index @property (nonatomic,assign) NSInteger index; @property (nonatomic,strong) UIView *footerView; @end @implementation MenuViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //添加拖动手势 UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panMenuWithPanGesture:)]; [self.view addGestureRecognizer:panGesture]; } #pragma mark - 添加手势 static CGPoint begin; static CGAffineTransform transform; - (void)panMenuWithPanGesture:(UIPanGestureRecognizer *)pan { ///位移量 CGPoint translateCGPoint = CGPointZero; ///开始手势时的位置 if (pan.state == UIGestureRecognizerStateBegan) { begin = [pan locationInView:self.view]; transform = self.centerViewController.view.transform; } ///手势过程中的位置 if (pan.state == UIGestureRecognizerStateChanged) { CGPoint end = [pan locationInView:self.view]; translateCGPoint = CGPointMake(end.x - begin.x, 0); } ///手势结束时的位置 if (pan.state == UIGestureRecognizerStateCancelled || pan.state == UIGestureRecognizerStateEnded) { if (self.centerViewController.view.transform.tx > kMenuLeftVCWidth * 0.5) { [self showLeftViewController]; } else { [self hideLeftViewController]; } return; } //手势过程中,对视图进行位移 if (self.centerViewController.view.transform.tx >= 0 && self.centerViewController.view.transform.tx <= kMenuLeftVCWidth) { self.centerViewController.view.transform = CGAffineTransformTranslate(transform, translateCGPoint.x, 0); self.leftTableView.transform = CGAffineTransformTranslate(transform, translateCGPoint.x, 0); } } #pragma mark - 初始化方法 static MenuViewController *MenuVC = nil; + (instancetype)instanceWithLeftViewController:(UIViewController *)leftVC CenterViewController:(UIViewController *)centerVC { @synchronized(self) { if (!MenuVC) { MenuVC = [[MenuViewController alloc]init]; //设置window的rootViewController [UIApplication sharedApplication].keyWindow.rootViewController = MenuVC; //设置视图属性 MenuVC.leftViewController = leftVC; MenuVC.centerViewController = centerVC; //添加子视图控制器和子视图 if (leftVC) { [MenuVC addChildViewController:leftVC]; [MenuVC.view addSubview:leftVC.view]; } else { [MenuVC setupLeftTableView]; } if (centerVC) { [MenuVC addChildViewController:centerVC]; [MenuVC.view addSubview:centerVC.view]; } } } return MenuVC; } #pragma mark - 获取抽屉的方法 + (instancetype)getMenuViewController { return MenuVC; } #pragma mark - 打开和关闭抽屉的方法 ///打开抽屉的方法 - (void)showLeftViewController { [self showLeftViewController:0.5]; } - (void)showLeftViewController:(CGFloat)duration { [UIView animateWithDuration:duration animations:^{ self.centerViewController.view.transform = CGAffineTransformMakeTranslation(kMenuLeftVCWidth, 0); self.leftTableView.transform = CGAffineTransformMakeTranslation(kMenuLeftVCWidth, 0); }]; } ///关闭抽屉的方法 - (void)hideLeftViewController { [self hideLeftViewController:0.5]; } - (void)hideLeftViewController:(CGFloat)duration { [UIView animateWithDuration:duration animations:^{ self.centerViewController.view.transform = CGAffineTransformMakeTranslation(0, 0); self.leftTableView.transform = CGAffineTransformMakeTranslation(0, 0); }]; } #pragma mark - 判断菜单展开的状态 - (BOOL)isShowing { if (self.centerViewController.view.transform.tx > 0) { return YES; } else { return NO; } } #pragma mark - 创建默认的菜单视图 - (void)setupLeftTableView { self.leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(- kMenuLeftVCWidth, 0, kMenuLeftVCWidth, self.view.frame.size.height) style:UITableViewStylePlain]; [self.view addSubview:self.leftTableView]; self.leftTableView.dataSource = self; self.leftTableView.delegate = self; self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.leftTableView.backgroundColor = [UIColor blackColor]; self.leftTableView.scrollEnabled = NO; [self createFooterView]; } - (void)createFooterView { UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 70, kMenuLeftVCWidth, 70)]; footerView.backgroundColor = [UIColor whiteColor]; [self.leftTableView addSubview:footerView]; self.footerView = footerView; } ///leftTableView的setter方法 - (void)setMenuArray:(NSMutableArray *)menuArray { if (_menuArray != menuArray) { _menuArray = nil; _menuArray = menuArray; [self.leftTableView reloadData]; } } #pragma mark - UITableViewDataSource //数据源的代理方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.menuArray.count; } // cell的样式 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *reuseID = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:reuseID]; } if (indexPath.row == 0) { cell.imageView.image = [UIImage imageNamed:@"头像.png"]; } cell.textLabel.text = self.menuArray[indexPath.row]; cell.backgroundColor = [UIColor clearColor]; cell.textLabel.textColor = [UIColor whiteColor]; cell.textLabel.alpha = 0.5; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.row == self.index) { cell.textLabel.alpha = 1; } return cell; } #pragma mark - 头部视图 // 头部高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 1; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kMenuLeftVCWidth, 70)]; tableView.tableHeaderView = headerView; //搜索按钮 UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; searchBtn.frame = CGRectMake(10, 0, 50, headerView.frame.size.height); [searchBtn setImage:[UIImage imageNamed:@"search.jpg"] forState:UIControlStateNormal]; [headerView addSubview:searchBtn]; //下载按钮 UIButton *downLoadBtn = [UIButton buttonWithType:UIButtonTypeCustom]; downLoadBtn.frame = CGRectMake(searchBtn.frame.origin.x + kMenuLeftVCWidth/4, 0, 50, headerView.frame.size.height); [downLoadBtn setImage:[UIImage imageNamed:@"downLoad.jpg"] forState:UIControlStateNormal]; [headerView addSubview:downLoadBtn]; //喜欢按钮 UIButton *hobbyBtn = [UIButton buttonWithType:UIButtonTypeCustom]; hobbyBtn.frame = CGRectMake(downLoadBtn.frame.origin.x + kMenuLeftVCWidth/4, 0, 50, headerView.frame.size.height); [hobbyBtn setImage:[UIImage imageNamed:@"hobby.jpg"] forState:UIControlStateNormal]; [headerView addSubview:hobbyBtn]; //其他按钮 UIButton *otherBtn = [UIButton buttonWithType:UIButtonTypeCustom]; otherBtn.frame = CGRectMake(hobbyBtn.frame.origin.x + kMenuLeftVCWidth/4, 0, 50, headerView.frame.size.height); [otherBtn setImage:[UIImage imageNamed:@"other.jpg"] forState:UIControlStateNormal]; [headerView addSubview:otherBtn]; return headerView; } #pragma mark - 尾部视图 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 2; } //- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { // UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 70, kMenuLeftVCWidth, 70)]; // footerView.backgroundColor = [UIColor grayColor]; // return footerView; //} #pragma mark - UITableViewDelegate //协议的代理方法 //点击cell的响应事件 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //取消选中状态 [tableView deselectRowAtIndexPath:indexPath animated:YES]; self.index = indexPath.row; [tableView reloadData]; #warning 3.执行相关的协议方法 if ([self.menuDelegate respondsToSelector:@selector(didClickMenuIndex:Title:)]) { [self.menuDelegate didClickMenuIndex:indexPath.row Title:self.menuArray[indexPath.row]]; } //关闭动画 [[MenuViewController getMenuViewController] hideLeftViewController]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end |
获取抽屉的方法 [MenuViewController instanceWithLeftViewController:nil CenterViewController:self.window.rootViewController]; |
#pragma mark - 点击导航栏左侧菜单按钮的响应事件 - (void)didClickBarBtnItem:(UIBarButtonItem *)sender { if ([MenuViewController getMenuViewController].isShowing) { [[MenuViewController getMenuViewController] hideLeftViewController]; } else { [[MenuViewController getMenuViewController] showLeftViewController]; } } |
标签:
原文地址:http://www.cnblogs.com/asdfg/p/5399346.html