// // PBDropdownMenu.m // IphoneBIMe // // Created by zjf on 2018/8/8. // Copyright © 2018年 ProBIM. All rights reserved. // #import "PBDropdownMenu.h" #import "PBIssueNavModel.h" #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height #define AnimateTime 0.25f // 下拉动画时间 @interface PBDropdownMenu() @property (nonatomic, strong) UIView *backgroundView;//背景视图 @property (nonatomic, strong) UITableView *tableView;//选择列表 @property (nonatomic, assign) BOOL isShow;//标识是否是弹出状态 @property (nonatomic, strong) NSMutableArray *selectIndexArr;//标识是否是弹出状态 @property (nonatomic, assign) NSInteger currentSelectIndex;//标识是否是弹出状态 @property (nonatomic, strong) NSMutableArray *buttonArr;//标识是否是弹出状态 @end @implementation PBDropdownMenu { CGFloat _rowHeight; // 下拉列表行高 NSArray *_titleArr; } - (void)setBtnTitle:(NSString *)title { UIButton *clickBtn = self.buttonArr[self.currentSelectIndex]; [clickBtn setTitle:title forState:UIControlStateNormal]; CGFloat imageWidth = clickBtn.imageView.bounds.size.width; CGFloat labelWidth = clickBtn.titleLabel.bounds.size.width; clickBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth); clickBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth); } - (instancetype)initWithFrame:(CGRect)frame andArr:(NSArray *)arr { if (self = [super initWithFrame:frame]) { _titleArr = arr; [self creatMenuListWithArr:arr]; } return self; } - (void)creatMenuListWithArr:(NSArray *)arr { _rowHeight = 40.f; CGFloat meunWidth = self.width / arr.count; for (NSInteger i = 0; i < arr.count; i++) { UIButton *clickBtn = [[UIButton alloc] init]; clickBtn.backgroundColor = [UIColor z_colorWithR:242 G:242 B:242]; [clickBtn setFrame:CGRectMake(meunWidth * i, 0, meunWidth, self.height)]; clickBtn.titleLabel.font = [UIFont systemFontOfSize:TitleFontSize]; [clickBtn setTitleColor:TitleColor forState:UIControlStateNormal]; PBIssueNavModel *issueNavModel = arr[i][0]; [clickBtn setImage:[UIImage imageNamed:@"Issue_ retrieve_default"] forState:UIControlStateNormal]; [clickBtn setTitle:issueNavModel.ItemName forState:UIControlStateNormal]; CGFloat imageWidth = clickBtn.imageView.bounds.size.width; CGFloat labelWidth = clickBtn.titleLabel.bounds.size.width; clickBtn.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth, 0, -labelWidth); clickBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWidth, 0, imageWidth); clickBtn.selected = NO; [clickBtn addTarget:self action:@selector(clickMainBtn:) forControlEvents:UIControlEventTouchUpInside]; clickBtn.tag = 100 + i; [self.buttonArr addObject:clickBtn]; [self addSubview:clickBtn]; } } #pragma mark - 点击方法 - (void)clickMainBtn:(UIButton *)button{ if (_currentSelectIndex == (button.tag - 100)) { if (_isShow) { [self hideDropDown]; }else { [self showDropDown]; } }else { if (_isShow) { [self hideDropDown]; }else { self.currentSelectIndex = button.tag - 100; [self showDropDown]; } } } #pragma mark - 显示下拉列表 - (void)showDropDown { [self.superview addSubview:self.backgroundView]; [self.backgroundView addSubview:self.tableView]; UIButton *clickBtn = self.buttonArr[self.currentSelectIndex]; [UIView animateWithDuration:AnimateTime animations:^{ clickBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI); NSArray *titles = self->_titleArr[self.currentSelectIndex]; CGFloat tableViewH = self->_rowHeight * titles.count; if (tableViewH > self->_rowHeight * 4) { tableViewH = self->_rowHeight * 4; } self->_tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, tableViewH); self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3]; }completion:^(BOOL finished) { self->_isShow = YES; }]; [clickBtn setTitleColor:WarningColor forState:UIControlStateNormal]; [clickBtn setImage:[UIImage imageNamed:@"Issue_ retrieve_show"] forState:UIControlStateNormal]; } #pragma mark - 隐藏下拉列表 - (void)hideDropDown{ UIButton *clickBtn = self.buttonArr[self.currentSelectIndex]; [UIView animateWithDuration:AnimateTime animations:^{ clickBtn.imageView.transform = CGAffineTransformIdentity; self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0]; self->_tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0); }completion:^(BOOL finished) { [self.backgroundView removeFromSuperview]; [self.tableView removeFromSuperview]; self->_isShow = NO; }]; [clickBtn setTitleColor:TitleColor forState:UIControlStateNormal]; [clickBtn setImage:[UIImage imageNamed:@"Issue_ retrieve_default"] forState:UIControlStateNormal]; } #pragma mark - UITableView Delegate -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return _rowHeight; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray *titles = _titleArr[self.currentSelectIndex]; return titles.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; NSArray *titles = _titleArr[self.currentSelectIndex]; NSInteger selectIndex = [self.selectIndexArr[self.currentSelectIndex] integerValue]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //---------------------------下拉选项样式,可在此处自定义------------------------- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.textLabel.font = [UIFont systemFontOfSize:16.f]; // cell.textLabel.textColor = [UIColor blackColor]; // cell.selectionStyle = UITableViewCellSelectionStyleNone; //--------------------------------------------------------------------------- } if (selectIndex == indexPath.row) { cell.textLabel.textColor = WarningColor; cell.selectionStyle = UITableViewCellSelectionStyleBlue; }else { cell.textLabel.textColor = TitleColor; cell.selectionStyle = UITableViewCellSelectionStyleNone; } PBIssueNavModel *issueNavModel = titles[indexPath.row]; cell.textLabel.text = issueNavModel.ItemName; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.selectIndexArr[self.currentSelectIndex] = [NSNumber numberWithInteger:indexPath.row]; NSArray *titles = _titleArr[self.currentSelectIndex]; PBIssueNavModel *issueNavModel = titles[indexPath.row]; [self setBtnTitle:issueNavModel.ItemName]; if ([self.delegate respondsToSelector:@selector(dropdownMenu:selectedCellNumber:)]) { [self.delegate dropdownMenu:self.buttonArr[self.currentSelectIndex] selectedCellNumber:indexPath.row]; } [self hideDropDown]; } - (UIView *)backgroundView { if (_backgroundView == nil) { _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0 , self.y + self.height, SCREEN_WIDTH, SCREEN_HEIGHT - self.y - self.height)]; _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0]; [_backgroundView setOpaque:NO]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTapped:)]; tap.delegate = self; [_backgroundView addGestureRecognizer:tap]; } return _backgroundView; } - (void)backTapped:(UITapGestureRecognizer *)sender { [self hideDropDown]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) { return NO; } else { return YES; } } - (UITableView *)tableView { _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,0)]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.bounces = NO; return _tableView; } - (NSMutableArray *)selectIndexArr { if (_selectIndexArr == nil) { _selectIndexArr = [[NSMutableArray alloc] init]; for (NSInteger i = 0; i < _titleArr.count; i++) { [_selectIndexArr addObject:[NSNumber numberWithInteger:0]]; } } return _selectIndexArr; } - (NSMutableArray *)buttonArr { if (_buttonArr == nil) { _buttonArr = [[NSMutableArray alloc] init]; } return _buttonArr; } @end