遵守代理:<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSArray *degreeArrays;
@property (nonatomic, strong) UIPickerView *degreePickerView;
@property (nonatomic, strong) NSString *degreeString;
// 时间 独用
@property (nonatomic, assign) NSInteger years; // 年
@property (nonatomic, assign) NSInteger months; // 月
@property (nonatomic, assign) NSInteger days; // 日
_degreePickerView = [[UIPickerView alloc] init];
_degreePickerView.delegate = self;
_degreePickerView.dataSource = self;
_degreePickerView.showsSelectionIndicator = YES;
[self.degreeContentView addSubview:_degreePickerView];
[self.degreePickerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.degreeContentView).insets(UIEdgeInsetsMake(self.view.frame.size.width/750*100, 0, self.view.frame.size.width/750*10, 0));
}];
// 设置pickerView背景,原理:pickerView背景色透明,在pickerView下边加一个带颜色的UIview
UIView *pickerBackView = [[UIView alloc] init];
pickerBackView.backgroundColor = RGBA(0, 0, 0, 0.4);
pickerBackView.layer.masksToBounds = YES;
pickerBackView.layer.cornerRadius = [UIScreen mainScreen].bounds.size.width/375*4;
[self.backView insertSubview:pickerBackView belowSubview:self.pickerView];
[pickerBackView makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.pickerView);
make.left.mas_equalTo(self.pickerView.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*20);
make.right.mas_equalTo(self.pickerView.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*20);
make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*40);
}];
/* // 添加图片或UIView:
_selectTopIcon = [[UIImageView alloc] init];
self.selectTopIcon.image = [UIImage imageNamed:@"pickerView_selected_dottedLine_icon"];
[self.pickerView addSubview:self.selectTopIcon];
[self.selectTopIcon makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.pickerView.centerY).offset(-[UIScreen mainScreen].bounds.size.width/375*19);
make.left.mas_equalTo(self.pickerView.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*0);
make.right.mas_equalTo(self.pickerView.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*0);
make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*2);
}];
_selectBottomIcon = [[UIImageView alloc] init];
self.selectBottomIcon.image = [UIImage imageNamed:@"pickerView_selected_dottedLine_icon"];
[self.pickerView addSubview:self.selectBottomIcon];
[self.selectBottomIcon makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.pickerView.centerY).offset([UIScreen mainScreen].bounds.size.width/375*19);
make.left.mas_equalTo(self.pickerView.mas_left).offset([UIScreen mainScreen].bounds.size.width/375*0);
make.right.mas_equalTo(self.pickerView.mas_right).offset(-[UIScreen mainScreen].bounds.size.width/375*0);
make.height.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*2);
}];
*/
UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
confirmButton.titleLabel.textAlignment = NSTextAlignmentCenter;
confirmButton.titleLabel.font = [UIFont systemFontOfSize:([UIScreen mainScreen].bounds.size.width / 375)*18 weight:UIFontWeightRegular];
[confirmButton setTitle:@"确定" forState:UIControlStateNormal];
[confirmButton setTitleColor:RGB(27, 167, 19, 1) forState:UIControlStateNormal];
[self.degreeContentView addSubview:confirmButton];
[confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.degreeContentView.mas_top).offset(self.view.frame.size.width/750*50);
make.right.mas_equalTo(self.degreeContentView).offset(-self.view.frame.size.width/750*70);
make.width.mas_equalTo(self.view.frame.size.width/750*100);
}];
[confirmButton addTarget:self action:@selector(confirmDegree) forControlEvents:UIControlEventTouchUpInside];
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.titleLabel.textAlignment = NSTextAlignmentCenter;
cancelButton.titleLabel.font = [UIFont systemFontOfSize:([UIScreen mainScreen].bounds.size.width / 375)*18 weight:UIFontWeightRegular];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor colorWithWhite:0.494 alpha:1] forState:UIControlStateNormal];
[self.degreeContentView addSubview:cancelButton];
[cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(confirmButton);
make.left.mas_equalTo(self.degreeContentView).offset(self.view.frame.size.width/750*70);
make.width.mas_equalTo(self.view.frame.size.width/750*100);
}];
[cancelButton addTarget:self action:@selector(cancelDegree) forControlEvents:UIControlEventTouchUpInside];
// 当 返回数据为nil时,用当前时间戳; 否则,用返回来的数据: self.timeString
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSDate *datenow = [NSDate date];
//----------将nsdate按formatter格式转成nsstring
NSString *currentTimeString = [formatter stringFromDate:datenow];
NSLog(@"当前的时间戳为: %@", currentTimeString);
NSLog(@"self.timeString = %@", self.timeString);
NSString *dateString = @"2019-10-12"; // 在这里,每次显示当前显示的时间(在这里固定了),应该将其写为:self.timeString
if (self.timeString.length > 0) {
dateString = self.timeString;
} else {
dateString = currentTimeString; // 当 返回的数据没有出生日期时,点击时间选择器,则以当前时间为显示时间
}
NSArray *array = [dateString componentsSeparatedByString:@"-"];
self.years = ((NSString *)array[0]).integerValue;
self.months = ((NSString *)array[1]).integerValue;
self.days = ((NSString *)array[2]).integerValue;
// 再次点击的时候,会停在 当前显示的时间
[_degreePickerView selectRow:self.years - 1919 inComponent:0 animated:NO];
[_degreePickerView selectRow:self.months - 1 inComponent:1 animated:NO];
[_degreePickerView selectRow:self.days - 1 inComponent:2 animated:NO];
代理方法:
#pragma mark -- UIPickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
// 该方法的返回值决定该控件包含的列数
NSInteger column = 0;
column = 3; // 时间选择器的列
return column;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// 该方法的返回值决定该控件指定列包含多少个列表项
if (self.cellIndexPath.row == 3) {
return self.degreeArrays.count; // 学历
} else { // 出生日期
if (component == 0) {
return 181; // 1919年 -- 2099年
} else if (component == 1) {
return 12;
} else {
NSInteger day = 31;
if (self.months == 1 || self.months == 3 || self.months == 5 || self.months == 7 || self.months == 8 || self.months == 10 || self.months == 12) {
day = 31;
} else if (self.months == 4 || self.months == 6 || self.months == 9 || self.months == 11) {
day = 30;
} else {
if ((self.years % 4 == 0 && self.years % 100 != 0) || self.years % 400 == 0) {
day = 29;
} else {
day = 28;
}
}
if (self.days > day) {
self.days = day;
}
return day;
}
}
}
#pragma mark -- UIPickerView Delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// 该方法的返回的NSString将作为UIPickerView中指定列和列表项的标题文本
if (self.cellIndexPath.row == 3) {
return [self.degreeArrays objectAtIndex:row];
} else {
if (component == 0) {
return [NSString stringWithFormat:@"%ld年", row + 1919];
} else if (component == 1) {
return [NSString stringWithFormat:@"%ld月", row + 1];
} else {
return [NSString stringWithFormat:@"%ld日", row + 1];
}
}
}
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated {
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
// 当选择某一列中的某一行的时候,会调用该方法(每滚动到一个地方就会调用一次)
if (self.cellIndexPath.row == 3) {
_degreeString = [self.degreeArrays objectAtIndex:row];
} else {
if (component == 0) { // 刷新 年
self.years = 1919 + row;
[pickerView reloadComponent:2];
} else if (component == 1) { // 刷新 月
self.months = row + 1;
[pickerView reloadComponent:2];
} else { // 刷新 日
self.days = row + 1;
}
// 将时间拼接起来
NSString *str1 = [NSString stringWithFormat:@"%ld", self.years];
NSString *str2 = [NSString stringWithFormat:@"%ld", self.months];
NSString *str3 = [NSString stringWithFormat:@"%ld", self.days];
NSArray *arr = @[str1, str2, str3];
self.timeString = [arr componentsJoinedByString:@"-"];
NSLog(@"选择时间 = %@", self.timeString);
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
// 设置组件的宽度
if (self.cellIndexPath.row == 3) {
return self.view.frame.size.width;
} else {
if (component == 0 || component == 2) {
return self.view.frame.size.width / 3 - 20;
} else {
return self.view.frame.size.width / 3 - 50;
}
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
// 设置组件中每一行的高度
return self.view.frame.size.width/750*100;
}
// 点击阴影部分,退出UIPickerView
- (void)backgroundViewHidden {
[self exitDegreeFunction];
}
// 选择器(文化程度)
- (void)confirmDegree {
[self exitDegreeFunction];
[self.tableView reloadData]; // 点击“确定”刷新tableView
}
- (void)cancelDegree {
[self exitDegreeFunction]; // 点击“取消”,就不用刷新tableView
}
// 隐藏PickerView
- (void)exitDegreeFunction {
[UIView animateWithDuration:0.3 animations:^{
self.degreeContentView.transform = CGAffineTransformMakeTranslation(0.01, self.view.frame.size.height);
self.backgroundView.hidden = YES;
} completion:^(BOOL finished) {
[self.backgroundView removeFromSuperview];
[self.degreeContentView removeFromSuperview];
}];
}
网友评论