// // MengBanView.m // LNManager // // Created by EchoShacolee on 2017/4/11. // Copyright © 2017年 lee. All rights reserved. // #import "MengBanView.h" #import "MBContentView.h" #define BtnHeight 40 #define Vertical_Space 20 @interface MengBanView () { MBContentView * _contentView; //对点击响应做了处理(超出contentview部分的点击) NSString * _title; //标题 NSArray *_btnsArr; //所有供选择btn的内容 NSInteger _count; //记录_contentView基本子视图个数(判断是否已经有待选择view) UIButton *_selectBtn; //记录当前待选择的btn NSMutableArray *_originalSelectArr; //show之前的所有btn初始值 MBBlock _block; //确定btn的回调 NSMutableArray *_selectArr; //确定回调的内容 } @end @implementation MengBanView -(void)setSelectArr:(NSMutableArray *)selectArr{ _selectArr = [selectArr mutableCopy]; for (int i=0; i<_originalSelectArr.count; i++) { UIButton *btn = (UIButton *)[_contentView viewWithTag:100+i]; [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal]; } } #pragma mark 选择框 -(instancetype)initWithTitileStr:(NSString *)title buttonsArray:(NSArray *)btnsArr block:(MBBlock)block{ self = [super init]; if (self) { self.frame = KWINDOW.bounds; _title = title; _btnsArr = btnsArr; _count = 0; _block = block; _selectArr = [NSMutableArray new]; for (NSArray * arr in btnsArr) { [_selectArr addObject:arr[0]]; } [self layoutAllSubviews]; } return self; } -(void)layoutAllSubviews{ /*创建灰色背景*/ UIView *bgView = [[UIView alloc] initWithFrame:KWINDOW.bounds]; bgView.backgroundColor = [UIColor colorWithWhite:.01 alpha:.4]; [self addSubview:bgView]; /*添加手势事件,移除View*/ UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)]; [bgView addGestureRecognizer:tapGesture]; /*创建显示View*/ NSInteger k = 0; if (_title.length != 0) { k++; } k += _btnsArr.count+1;//+1 是因为取消,确定 _contentView = [[MBContentView alloc] initWithFrame:CGRectMake(0, 0, kSize.width*280/320, (BtnHeight+Vertical_Space)*k+Vertical_Space)];//多加一个间隔也是给取消确定用的(ps:有标题的情况高度就不作处理了 这样上下距离感还好) _contentView.center = self.center; _contentView.backgroundColor=RGB_COLOR(246, 246, 246); _contentView.alpha = 1.0; _contentView.layer.cornerRadius = 10; // _contentView.layer.masksToBounds = YES; [self addSubview:_contentView]; CGSize contentSize = _contentView.frame.size; CGFloat x = 20; CGFloat y = Vertical_Space; CGFloat w = contentSize.width-2*20; CGFloat h = BtnHeight; if (_title.length != 0) { UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, w, h)]; label.text = _title; label.textAlignment = NSTextAlignmentCenter; label.textColor = [UIColor blackColor]; label.font = [UIFont fontWithName:@"Helvetica-Bold" size:17]; [_contentView addSubview:label]; _count++; // y += BtnHeight; } for (int i=0; i<_btnsArr.count; i++) { UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)]; button.backgroundColor = [UIColor whiteColor]; button.layer.cornerRadius = 4; button.layer.borderWidth = .5f; button.layer.borderColor = CGCOLOR_LINE; button.titleLabel.font = [UIFont systemFontOfSize:15]; [button setTitle:_btnsArr[i][0] forState:UIControlStateNormal]; [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; // button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; button.tag=100+i; [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_contentView addSubview:button]; _count++; if (i != _btnsArr.count-1) { y += Vertical_Space+BtnHeight; } } x = 40; y += 30+BtnHeight; w = (contentSize.width-40*3)/2; UIButton *button3 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)]; button3.layer.cornerRadius = 4; button3.layer.borderWidth = .5f; button3.layer.borderColor = CGCOLOR_LINE; [button3 setTitle:@"取消" forState:UIControlStateNormal]; [button3 setTitleColor:COLOR_THEME forState:UIControlStateNormal]; button3.tag = 10; [button3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_contentView addSubview:button3]; _count++; x = 40*2+w; UIButton *button4 = [[UIButton alloc]initWithFrame:CGRectMake(x, y, w, h)]; button4.layer.cornerRadius = 4; button4.layer.borderWidth = .5f; button4.layer.borderColor = CGCOLOR_LINE; [button4 setTitle:@"确定" forState:UIControlStateNormal]; [button4 setTitleColor:COLOR_THEME forState:UIControlStateNormal]; button4.tag = 11; [button4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_contentView addSubview:button4]; _count++; } -(void)btnClick:(UIButton *)sender{ switch (sender.tag) { case 10: { [self dismissContactViewWithIsSure:NO]; } break; case 11: { //确定 [self dismissContactViewWithIsSure:YES]; if (_block) { _block(_selectArr); } } break; default: { NSArray * titles = _btnsArr[sender.tag-100]; [self creatMenuWithTitles:titles Frame:sender.frame]; _selectBtn = sender; } break; } } -(void)creatMenuWithTitles:(NSArray *)titles Frame:(CGRect)frame{ if (_contentView.subviews.count > _count) { [[_contentView.subviews lastObject] removeFromSuperview]; } CGFloat height = 44; UIView * view = [[UIView alloc]initWithFrame:CGRectMake(frame.origin.x+10, frame.origin.y, frame.size.width-20, height*titles.count)]; view.backgroundColor = RGB_COLOR(231, 231, 231); view.alpha = 1.0; for (int i=0; i _btnsArr.count - 1) { return; } if (index > [_btnsArr[section] count] - 1) { return; } NSString *title = _btnsArr[section][index]; UIButton *btn = (UIButton *)[_contentView viewWithTag:100+section]; [btn setTitle:title forState:UIControlStateNormal]; //保存默认选中按钮 [_selectArr replaceObjectAtIndex:section withObject:title]; } #pragma mark 自定义内容视图Frame:(CGRect)frame -(instancetype)initWithContentView:(UIView *)contentView{ if (self = [super init]) { self.frame = KWINDOW.bounds; /*创建灰色背景*/ UIView *bgView = [[UIView alloc] initWithFrame:KWINDOW.bounds]; bgView.backgroundColor = [UIColor colorWithWhite:.01 alpha:.4]; [self addSubview:bgView]; /*添加手势事件,移除View*/ UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissContactView:)]; [bgView addGestureRecognizer:tapGesture]; //添加传入的内容视图 [self addSubview:contentView]; } return self; } #pragma mark - 手势点击事件,移除View - (void)dismissContactView:(UITapGestureRecognizer *)tapGesture{ [self dismissContactViewWithIsSure:NO]; } -(void)dismissContactViewWithIsSure:(BOOL)sure { if (_contentView.subviews.count > _count) { [[_contentView.subviews lastObject] removeFromSuperview]; return; } //如果是取消 if (!sure) { //取消(取消之前恢复按钮原有选取) _selectArr = [_originalSelectArr mutableCopy]; for (int i=0; i<_originalSelectArr.count; i++) { UIButton *btn = (UIButton *)[_contentView viewWithTag:100+i]; [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal]; } } __weak typeof(self)weakSelf = self; [UIView animateWithDuration:0.5 animations:^{ weakSelf.alpha = 0; } completion:^(BOOL finished) { [weakSelf removeFromSuperview]; }]; } // 这里加载在了window上 -(void)showView { _originalSelectArr = [_selectArr mutableCopy];//记录按钮初始选择,用以点击取消的时候恢复初始选择 self.alpha = 1.0; UIWindow * window = [UIApplication sharedApplication].windows[0]; [window addSubview:self]; } @end