// // PickerChoiceView.m // TFPickerView // // Created by TF_man on 16/5/11. // Copyright © 2016年 tituanwang. All rights reserved. // //屏幕宽和高 #define kScreenWidth ([UIScreen mainScreen].bounds.size.width) #define kScreenHeight ([UIScreen mainScreen].bounds.size.height) //RGB #define RGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)] // 缩放比 #define kScale ([UIScreen mainScreen].bounds.size.width) / 375 #define hScale ([UIScreen mainScreen].bounds.size.height) / 667 //字体大小 #define kfont 15 #import "PickerChoiceView.h" //#import "Masonry.h" @interface PickerChoiceView () @property (nonatomic,strong)UIView *bgV; @property (nonatomic,strong)UIButton *cancelBtn; @property (nonatomic,strong)UIButton *conpleteBtn; @property (nonatomic,strong)UIPickerView *pickerV; @property (nonatomic,strong)NSMutableArray *array; @end @implementation PickerChoiceView - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.array = [NSMutableArray array]; self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight); self.backgroundColor = RGBA(51, 51, 51, 0.8); self.bgV = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 260*hScale)]; self.bgV.backgroundColor = [UIColor whiteColor]; [self addSubview:self.bgV]; [self showAnimation]; //取消 self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.bgV addSubview:self.cancelBtn]; self.cancelBtn.sd_layout .topSpaceToView(self.bgV,0) .leftSpaceToView(self.bgV,15) .widthIs(40) .heightIs(44); self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:kfont]; [self.cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; [self.cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self.cancelBtn setTitleColor:RGBA(0, 122, 255, 1) forState:UIControlStateNormal]; //完成 self.conpleteBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [self.bgV addSubview:self.conpleteBtn]; self.conpleteBtn.sd_layout .topSpaceToView(self.bgV,0) .rightSpaceToView(self.bgV,15) .widthIs(40) .heightIs(44); self.conpleteBtn.titleLabel.font = [UIFont systemFontOfSize:kfont]; [self.conpleteBtn setTitle:@"完成" forState:UIControlStateNormal]; [self.conpleteBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside]; [self.conpleteBtn setTitleColor:RGBA(0, 122, 255, 1) forState:UIControlStateNormal]; //选择titi self.selectLb = [UILabel new]; [self.selectLb setSingleLineAutoResizeWithMaxWidth:0]; [self.bgV addSubview:self.selectLb]; self.selectLb.sd_layout .centerXEqualToView(self.bgV) .centerYEqualToView(self.conpleteBtn) .heightIs(44); self.selectLb.font = [UIFont systemFontOfSize:kfont]; self.selectLb.textAlignment = NSTextAlignmentCenter; //线 UIView *line = [UIView new]; [self.bgV addSubview:line]; line.sd_layout .leftEqualToView(self.bgV) .topSpaceToView(self.conpleteBtn,0) .rightEqualToView(self.bgV) .widthIs(SCREEN_WIDTH) .heightIs(0.5); line.backgroundColor = RGBA(224, 224, 224, 1); //选择器 self.pickerV = [UIPickerView new]; [self.bgV addSubview:self.pickerV]; self.pickerV.sd_layout .topSpaceToView(line,0) .bottomEqualToView(self.bgV) .leftEqualToView(self.bgV) .rightEqualToView(self.bgV); self.pickerV.delegate = self; self.pickerV.dataSource = self; } return self; } - (void)setCustomArr:(NSArray *)customArr{ _customArr = customArr; [self.array addObject:customArr]; } - (void)setArrayType:(ARRAYTYPE)arrayType { _arrayType = arrayType; switch (arrayType) { case GenderArray: { self.selectLb.text = @"选择性别"; [self.array addObject:@[@"男",@"女"]]; } break; case HeightArray: { self.selectLb.text = @"选择身高"; NSMutableArray *arr = [NSMutableArray array]; for (int i = 100; i <= 250; i++) { NSString *str = [NSString stringWithFormat:@"%d",i]; [arr addObject:str]; } [self.array addObject:(NSArray *)arr]; } break; case weightArray: { self.selectLb.text = @"选择体重"; NSMutableArray *arr = [NSMutableArray array]; for (int i = 30; i <= 200; i++) { NSString *str = [NSString stringWithFormat:@"%d",i]; [arr addObject:str]; } [self.array addObject:(NSArray *)arr]; } break; case DeteArray: { self.selectLb.text = @"选择出生年月"; [self creatDate]; } break; default: break; } } - (void)creatDate{ NSMutableArray *yearArray = [[NSMutableArray alloc] init]; for (int i = 1970; i <= 2050 ; i++) { [yearArray addObject:[NSString stringWithFormat:@"%d年",i]]; } [self.array addObject:yearArray]; NSMutableArray *monthArray = [[NSMutableArray alloc]init]; for (int i = 1; i < 13; i ++) { [monthArray addObject:[NSString stringWithFormat:@"%d月",i]]; } [self.array addObject:monthArray]; NSMutableArray *daysArray = [[NSMutableArray alloc]init]; for (int i = 1; i < 32; i ++) { [daysArray addObject:[NSString stringWithFormat:@"%d日",i]]; } [self.array addObject:daysArray]; NSDate *date = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy"]; NSString *currentYear = [NSString stringWithFormat:@"%@年",[formatter stringFromDate:date]]; [self.pickerV selectRow:[yearArray indexOfObject:currentYear]+81*50 inComponent:0 animated:YES]; [formatter setDateFormat:@"MM"]; NSString *currentMonth = [NSString stringWithFormat:@"%ld月",(long)[[formatter stringFromDate:date]integerValue]]; [self.pickerV selectRow:[monthArray indexOfObject:currentMonth]+12*50 inComponent:1 animated:YES]; [formatter setDateFormat:@"dd"]; NSString *currentDay = [NSString stringWithFormat:@"%@日",[formatter stringFromDate:date]]; [self.pickerV selectRow:[daysArray indexOfObject:currentDay]+31*50 inComponent:2 animated:YES]; } #pragma mark-----UIPickerViewDataSource - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return self.array.count; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ NSArray * arr = (NSArray *)[self.array objectAtIndex:component]; if (self.arrayType == DeteArray) { return arr.count*100; }else{ return arr.count; } } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ UILabel *label=[[UILabel alloc] init]; label.textAlignment = NSTextAlignmentCenter; label.text=[self pickerView:pickerView titleForRow:row forComponent:component]; return label; } - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ NSArray *arr = (NSArray *)[self.array objectAtIndex:component]; return [arr objectAtIndex:row % arr.count]; } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { if (self.arrayType == DeteArray) { return 60; }else{ return 110; } } #pragma mark-----点击方法 - (void)cancelBtnClick{ [self hideAnimation]; } - (void)completeBtnClick{ NSString *fullStr = [NSString string]; for (int i = 0; i < self.array.count; i++) { NSArray *arr = [self.array objectAtIndex:i]; if (self.arrayType == DeteArray) { NSString *str = [arr objectAtIndex:[self.pickerV selectedRowInComponent:i]% arr.count]; fullStr = [fullStr stringByAppendingString:str]; }else{ NSString *str = [arr objectAtIndex:[self.pickerV selectedRowInComponent:i]]; fullStr = [fullStr stringByAppendingString:str]; } } [self.delegate PickerSelectorIndixString:fullStr]; [self hideAnimation]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self hideAnimation]; } //隐藏动画 - (void)hideAnimation{ [UIView animateWithDuration:0.5 animations:^{ CGRect frame = self.bgV.frame; frame.origin.y = kScreenHeight; self.bgV.frame = frame; } completion:^(BOOL finished) { [self.bgV removeFromSuperview]; [self removeFromSuperview]; }]; } //显示动画 - (void)showAnimation{ [UIView animateWithDuration:0.5 animations:^{ CGRect frame = self.bgV.frame; frame.origin.y = kScreenHeight-260*hScale; self.bgV.frame = frame; }]; } @end