// // MengBanView.m // LNManager // // Created by EchoShacolee on 2017/4/11. // Copyright © 2017年 lee. All rights reserved. // #import "MengBanView.h" #import "MBContentView.h" #import "DateView.h" #define BtnHeight 40 #define Vertical_Space 20 #define KWINDOW [UIApplication sharedApplication].keyWindow #define CGCOLOR_LINE [UIColor colorWithRed:136/255.0f green:136/255.0f blue:136/255.0f alpha:1].CGColor @interface MengBanView () { BOOL _isAllbtn; 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 #pragma mark 初始化方法 -(instancetype)initWithTitileStr:(NSString *)title buttonsArray:(NSArray *)btnsArr isAllbtn:(BOOL)isAllbtn block:(MBBlock)block{ self = [super init]; if (self) { self.frame = KWINDOW.bounds; _title = title; _btnsArr = btnsArr; _isAllbtn = isAllbtn; _count = 0; _block = block; _selectArr = [NSMutableArray new]; if (isAllbtn) { [self layoutAllSubviews];//清一色button for (NSArray * arr in btnsArr) { [_selectArr addObject:arr[0]]; } }else{ [self layoutDifferentSubviews];//大杂烩 for (NSArray * arr in btnsArr) { if ([arr[0] isEqualToString:@"1"]) { [_selectArr addObject:arr[1][0]]; }else if([arr[0] isEqualToString:@"2"]){ [_selectArr addObject:arr[1]]; }else if([arr[0] isEqualToString:@"3"]){ [_selectArr addObject:@""]; } } } } return self; } #pragma mark 布局 -(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:defGreen 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:defGreen forState:UIControlStateNormal]; button4.tag = 11; [button4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_contentView addSubview:button4]; _count++; } -(void)layoutDifferentSubviews{ /*创建灰色背景*/ 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; } int i=0; for (NSArray * obj in _btnsArr) { //1button 2selcdate 3textfiled, if ([obj[0] isEqualToString:@"3"]) { UITextField * tf = obj[1]; tf.frame = CGRectMake(x, y, w, h); tf.backgroundColor = [UIColor whiteColor]; tf.delegate = self; tf.tag = 1000+i; [_contentView addSubview:tf]; }else{ 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]; id theObj = _btnsArr[i][1]; if ([obj[0] isEqualToString:@"2"]) { [button setTitle:theObj forState:UIControlStateNormal]; button.tag=4399+i;//日期选择 }else{ [button setTitle:theObj[0] forState:UIControlStateNormal]; button.tag=1+i; } [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal]; // button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_contentView addSubview:button]; } _count++; if (i != _btnsArr.count-1) { y += Vertical_Space+BtnHeight; } i++; } 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:defGreen 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:defGreen forState:UIControlStateNormal]; button4.tag = 11; [button4 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_contentView addSubview:button4]; _count++; } #pragma mark 点击事件 -(void)btnClick:(UIButton *)sender{ for (id obj in _contentView.subviews) { if ([obj isKindOfClass:[UITextField class]] && [obj isFirstResponder]) { [obj resignFirstResponder]; } } switch (sender.tag) { case 10: { //取消 [self dismissContactViewWithIsSure:NO]; } break; case 11: { //确定 [self dismissContactViewWithIsSure:YES]; if (_block) { _block(_selectArr); } } break; default: { if (sender.tag >= 4399) { //选择日期 [self creatDateView]; }else{ NSArray * titles; if (sender.tag < 10) { //deifferent titles = _btnsArr[sender.tag-1][1]; }else{ //All button titles = _btnsArr[sender.tag-100]; } [self creatMenuWithTitles:titles Frame:sender.frame]; } _selectBtn = sender; } break; } } -(void)creatDateView{ if (_contentView.subviews.count > _count) { [[_contentView.subviews lastObject] removeFromSuperview]; } DateView *dateV = [[DateView alloc] init]; [dateV setStyle:0]; [dateV showWithComplete:^(NSString * result) { //更新UI [_selectBtn setTitle:result forState:UIControlStateNormal]; //更新返回内容 [_selectArr replaceObjectAtIndex:_selectBtn.tag-4399 withObject:result]; }]; } -(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= 100) { [_selectArr replaceObjectAtIndex:_selectBtn.tag-100 withObject:sender.currentTitle]; }else{ [_selectArr replaceObjectAtIndex:_selectBtn.tag-1 withObject:sender.currentTitle]; } } #pragma mark UITextFiled代理 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSString *newStr = [textField.text stringByReplacingCharactersInRange:range withString:string]; [_selectArr replaceObjectAtIndex:textField.tag-1000 withObject:newStr]; return YES; } #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{ //如果有键盘弹出的话,就隐藏键盘好了 for (id obj in _contentView.subviews) { if ([obj isKindOfClass:[UITextField class]] && [obj isFirstResponder]) { [obj resignFirstResponder]; return; } } [self dismissContactViewWithIsSure:NO]; } -(void)dismissContactViewWithIsSure:(BOOL)sure { if (_contentView.subviews.count > _count) { [[_contentView.subviews lastObject] removeFromSuperview]; return; } //如果是取消 if (!sure) { //取消(取消之前恢复按钮原有选取) _selectArr = [_originalSelectArr mutableCopy]; //判断是否全是button if (_isAllbtn) { for (int i=0; i<_originalSelectArr.count; i++) { UIButton *btn = (UIButton *)[_contentView viewWithTag:100+i]; [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal]; } }else{ for (int i=0; i<_originalSelectArr.count; i++) { if ([_btnsArr[i][0] isEqualToString:@"1"]) { UIButton *btn = (UIButton *)[_contentView viewWithTag:1+i]; [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal]; }else if ([_btnsArr[i][0] isEqualToString:@"2"]){ //date UIButton *btn = (UIButton *)[_contentView viewWithTag:4399+i]; [btn setTitle:_originalSelectArr[i] forState:UIControlStateNormal]; }else if ([_btnsArr[i][0] isEqualToString:@"3"]){ //textfiled UITextField * tf = (UITextField *)[_contentView viewWithTag:1000+i]; tf.text = _originalSelectArr[i]; } } } } __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 = myDelegate.window; [window addSubview:self]; } @end