// // YOUPAIHRSelectDayDateVC.m // VQU // // Created by xiaohaoran on 2021/11/5. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAIHRSelectDayDateVC.h" #import "BRPickerView.h" typedef NS_ENUM(NSInteger, BRTimeType) { BRTimeTypeBeginTime = 0, BRTimeTypeEndTime }; @interface YOUPAIHRSelectDayDateVC () @property (nonatomic, strong) BRDatePickerView *youpaipdatePickerView; @property (nonatomic, strong) NSDate *youpaipbeginSelectDate; @property (nonatomic, strong) NSDate *youpaipendSelectDate; @property (nonatomic, strong) UITextField *youpaipbeginTimeTF; @property (nonatomic, strong) UITextField *youpaipendTimeTF; @property (nonatomic, assign) BRTimeType youpaiptimeType; @end @implementation YOUPAIHRSelectDayDateVC -(void)viewDidLoad{ [super viewDidLoad]; [self youpaifsetupUI]; } #pragma mark - UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { [self.view endEditing:YES]; [self youpaifhandlerTextFieldSelect:textField]; return NO; // 当前 textField 不可编辑,可以响应点击事件 } - (void)youpaifhandlerTextFieldSelect:(UITextField *)textField { if (textField.tag == 100) { self.youpaiptimeType = BRTimeTypeBeginTime; self.youpaipendTimeTF.textColor = HexColorFromRGBA(0xFFFFFF,0.2); self.youpaipbeginTimeTF.textColor = [UIColor whiteColor]; NSString *format = @"yyyy-MM-dd"; if (self.youpaipdatePickerView.pickerMode == BRDatePickerModeYM) { format = @"yyyy-MM"; } else if (self.youpaipdatePickerView.pickerMode == BRDatePickerModeYMDH) { format = @"yyyy-MM-dd HH"; } // 设置选择器滚动到指定的日期 //self.youpaipdatePickerView.selectValue = self.youpaipbeginTimeTF.text; self.youpaipdatePickerView.selectDate = self.youpaipbeginSelectDate; }else{ self.youpaiptimeType = BRTimeTypeEndTime; self.youpaipbeginTimeTF.textColor = HexColorFromRGBA(0xFFFFFF,0.2); self.youpaipendTimeTF.textColor = [UIColor whiteColor]; NSString *format = @"yyyy-MM-dd"; if (self.youpaipdatePickerView.pickerMode == BRDatePickerModeYM) { format = @"yyyy-MM"; } else if (self.youpaipdatePickerView.pickerMode == BRDatePickerModeYMDH) { format = @"yyyy-MM-dd HH"; } // 设置选择器滚动到指定的日期 //self.youpaipdatePickerView.selectValue = self.youpaipendTimeTF.text; self.youpaipdatePickerView.selectDate = self.youpaipendSelectDate; } } -(void)youpaifsetupUI{ UIView *bottomView = [UIView new]; [self.view addSubview:bottomView]; [bottomView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.offset(0.0f); make.height.offset(324+SafeHeight-49); }]; bottomView.backgroundColor = LCBkgColor; //youpaipheaderView UIView *youpaipheaderView = [UIView new]; [bottomView addSubview:youpaipheaderView]; [youpaipheaderView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.top.mas_equalTo(0); make.height.mas_equalTo(60); }]; youpaipheaderView.backgroundColor = HexColorFromRGBA(0x4F4B5B, 0.2); //leftView UIView *leftBgView = [UIView new]; [youpaipheaderView addSubview:leftBgView]; [leftBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.width.mas_equalTo(KScreenWidth/2); make.top.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; leftBgView.backgroundColor = [UIColor clearColor]; //title UILabel *beginTimeLabel = [UILabel new]; [leftBgView addSubview:beginTimeLabel]; [beginTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(0); make.top.mas_equalTo(14); make.width.mas_equalTo(120); }]; beginTimeLabel.text = @"开始时间"; beginTimeLabel.font = [UIFont systemFontOfSize:14]; beginTimeLabel.textColor = [UIColor whiteColor]; beginTimeLabel.textAlignment = NSTextAlignmentCenter; //时间 UITextField *youpaipbeginTimeTF = [UITextField new]; self.youpaipbeginTimeTF = youpaipbeginTimeTF; [leftBgView addSubview:youpaipbeginTimeTF]; [youpaipbeginTimeTF mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(0); make.top.mas_equalTo(beginTimeLabel.mas_bottom).offset(6); make.width.mas_equalTo(120); }]; youpaipbeginTimeTF.delegate = self; youpaipbeginTimeTF.tag = 100; youpaipbeginTimeTF.textAlignment = NSTextAlignmentCenter; NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"选择时间" attributes: @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:youpaipbeginTimeTF.font }]; youpaipbeginTimeTF.attributedPlaceholder = attrString; youpaipbeginTimeTF.textColor = [UIColor whiteColor]; youpaipbeginTimeTF.font = [UIFont systemFontOfSize:11]; youpaipbeginTimeTF.textAlignment = NSTextAlignmentCenter; //leftView UIView *rightBgView = [UIView new]; [youpaipheaderView addSubview:rightBgView]; [rightBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(0); make.width.mas_equalTo(KScreenWidth/2); make.top.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; rightBgView.backgroundColor = [UIColor clearColor]; //title UILabel *endTimeLabel = [UILabel new]; [rightBgView addSubview:endTimeLabel]; [endTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(0); make.top.mas_equalTo(14); make.width.mas_equalTo(120); }]; endTimeLabel.text = @"结束时间"; endTimeLabel.font = [UIFont systemFontOfSize:14]; endTimeLabel.textColor = [UIColor whiteColor]; endTimeLabel.textAlignment = NSTextAlignmentCenter; //时间 UITextField *youpaipendTimeTF = [UITextField new]; self.youpaipendTimeTF = youpaipendTimeTF; [rightBgView addSubview:youpaipendTimeTF]; [youpaipendTimeTF mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(0); make.top.mas_equalTo(endTimeLabel.mas_bottom).offset(6); make.width.mas_equalTo(120); }]; youpaipendTimeTF.delegate = self; youpaipendTimeTF.tag = 101; youpaipendTimeTF.textAlignment = NSTextAlignmentCenter; NSAttributedString *attrString1 = [[NSAttributedString alloc] initWithString:@"选择时间" attributes: @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:youpaipendTimeTF.font }]; youpaipendTimeTF.attributedPlaceholder = attrString1; youpaipendTimeTF.textColor = [UIColor whiteColor]; youpaipendTimeTF.font = [UIFont systemFontOfSize:11]; youpaipendTimeTF.textAlignment = NSTextAlignmentCenter; // 创建选择器容器视图 UIView *containerView = [[UIView alloc]init]; [bottomView addSubview:containerView]; [containerView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(youpaipheaderView.mas_bottom); make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.bottom.mas_equalTo(-SafeHeight); }]; containerView.backgroundColor = LCBkgColor; // 4.创建日期选择器 BRDatePickerView *youpaipdatePickerView = [[BRDatePickerView alloc]init]; youpaipdatePickerView.pickerMode = BRDatePickerModeYMD; youpaipdatePickerView.maxDate = [NSDate date]; youpaipdatePickerView.isAutoSelect = YES; youpaipdatePickerView.showUnitType = BRShowUnitTypeOnlyCenter; youpaipdatePickerView.resultBlock = ^(NSDate *youpaipselectDate, NSString *selectValue) { if (self.youpaiptimeType == BRTimeTypeBeginTime) { self.youpaipbeginSelectDate = youpaipselectDate; self.youpaipbeginTimeTF.text = selectValue; } else if (self.youpaiptimeType == BRTimeTypeEndTime) { self.youpaipendSelectDate = youpaipselectDate; self.youpaipendTimeTF.text = selectValue; } }; containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth; // 自定义选择器主题样式 BRPickerStyle *customStyle = [[BRPickerStyle alloc]init]; customStyle.pickerColor = [UIColor clearColor]; youpaipdatePickerView.pickerStyle = customStyle; self.youpaipdatePickerView = youpaipdatePickerView; customStyle.pickerTextColor = HexColorFromRGBA(0xFFFFFF, 0.2); customStyle.separatorColor = [UIColor clearColor]; customStyle.selectRowTextColor = [UIColor whiteColor]; customStyle.dateUnitTextColor = [UIColor whiteColor]; // 添加选择器到容器视图 [youpaipdatePickerView addPickerToView:containerView]; //确定按钮 UIButton *submitBtn = [UIButton new]; [self.view addSubview:submitBtn]; [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(0); make.bottom.mas_equalTo(-SafeHeight); make.size.mas_equalTo(CGSizeMake(294, 49)); }]; [submitBtn setBackgroundImage:[LCTools ColorImage:CGSizeMake(294, 49) FromColors:@[ZYGradientOneColor,ZYGradientTwoColor] ByGradientType:1] forState:UIControlStateNormal]; [submitBtn setTitle:@"确定" forState:UIControlStateNormal]; [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; submitBtn.titleLabel.font = LCBoldFont(16); [submitBtn addTarget:self action:@selector(youpaifsubmitBtnClick) forControlEvents:UIControlEventTouchUpInside]; submitBtn.layer.cornerRadius = 49/2; submitBtn.layer.masksToBounds = YES; } -(void)youpaifsubmitBtnClick{ if (self.youpaipbeginSelectDate == nil) { [ZCHUDHelper showTitle:@"请选择开始时间"]; return; } if (self.youpaipendSelectDate == nil) { [ZCHUDHelper showTitle:@"请选择结束时间"]; return; } if ([self youpaifcompareDate:self.youpaipbeginTimeTF.text withDate:self.youpaipendTimeTF.text] == 1) { }else{ [ZCHUDHelper showTitle:@"结束时间要大于开始时间"]; } if (self.youpaipdidSelectTimeBlock) { self.youpaipdidSelectTimeBlock(self.youpaipbeginTimeTF.text, self.youpaipendTimeTF.text); } } /** 比较两个日期的大小 日期格式为:yyyy-MM-dd HH-mm */ -(int)youpaifcompareDate:(NSString*)date01 withDate:(NSString*)date02{ int ci; NSDateFormatter *df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd"]; NSDate *dt1 = [[NSDate alloc] init]; NSDate *dt2 = [[NSDate alloc] init]; dt1 = [df dateFromString:date01]; dt2 = [df dateFromString:date02]; NSComparisonResult result = [dt1 compare:dt2]; switch (result) { //date02比date01大 case NSOrderedAscending: ci=1; break; //date02比date01小 case NSOrderedDescending: ci=-1; break; //date02=date01 // case NSOrderedSame: ci=0; break; default: NSLog(@"erorr dates %@, %@", dt2, dt1); break; } return ci; } #pragma mark - JXCategoryListContentViewDelegate - (UIView *)listView{ return self.view; } @end