// // ZCDatePickerView.m // UniversalApp // // Created by 凯朱 on 2019/6/1. // Copyright © 2019 徐阳. All rights reserved. // #import "ZCDatePickerView.h" @interface ZCDatePickerView() { NSString *_startTime; NSString *_endTime; } @property (nonatomic, strong) UIButton *cancelBtn; @property (nonatomic, strong) UIButton *confirmBtn; @property (nonatomic, strong) UIView *naviContainView; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UIButton *startDateBtn; @property (nonatomic, strong) UIView *startLine; @property (nonatomic, strong) UIView *endLine; @property (nonatomic, strong) UIButton *endDateBtn; @property (nonatomic, strong) UIDatePicker *datePicker; @end @implementation ZCDatePickerView - (instancetype)init{ self = [super init]; if (self) { [self setupChildViews]; _startTime = [NSString dateStrFromDate:[NSDate date] formatStr:@"yyyy-MM-dd"]; _startDateBtn.selected = YES; [self updateSubViews]; } return self; } - (void)setupChildViews { [self addSubview:self.naviContainView]; [self.naviContainView addSubview:self.cancelBtn]; [self.naviContainView addSubview:self.titleLabel]; [self.naviContainView addSubview:self.confirmBtn]; [self addSubview:self.startDateBtn]; [self addSubview:self.startLine]; [self addSubview:self.endDateBtn]; [self addSubview:self.endLine]; [self addSubview:self.datePicker]; UILabel *label = [[UILabel alloc] init]; label.text = @"至"; label.textAlignment = NSTextAlignmentCenter; label.textColor = KColor_2; label.font = kFontMedium(16); [self addSubview:label]; self.frame = CGRectMake(0, KScreenHeight, kScreenWidth,KScreenHeight-20); self.backgroundColor = KWhiteColor; [self.naviContainView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self); make.height.mas_equalTo(44); }]; [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.centerY.equalTo(self.naviContainView); }]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.naviContainView); }]; [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-15); make.centerY.equalTo(self.naviContainView); }]; CGFloat width = (KScreenWidth-84)/2; [self.startDateBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(97); make.left.equalTo(self).offset(15); make.size.mas_equalTo(CGSizeMake(width, 33)); }]; [self.startLine mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.startDateBtn.mas_bottom).offset(5); make.left.equalTo(self).offset(15); make.size.mas_equalTo(CGSizeMake(width, 1)); }]; [self.endDateBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(97); make.right.equalTo(self).offset(-15); make.size.mas_equalTo(CGSizeMake(width, 33)); }]; [self.endLine mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.endDateBtn.mas_bottom).offset(5); make.right.equalTo(self).offset(-15); make.size.mas_equalTo(CGSizeMake(width, 1)); }]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self); make.centerY.equalTo(self.startDateBtn); make.size.mas_equalTo(CGSizeMake(54, 20)); }]; [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.startLine.mas_bottom).mas_equalTo(17); make.left.right.equalTo(self); make.height.mas_equalTo(264); }]; } -(void)setMaximumDate:(NSDate *)maximumDate{ _maximumDate = maximumDate; _datePicker.maximumDate = maximumDate; } #pragma mark - private methods - (void)cancelAction:(UIButton *)btn { [self dismiss]; } - (void)confirmAction:(UIButton *)btn { if (_endTime.length == 0) { _endTime = [NSString dateStrFromDate:[NSDate date] formatStr:@"yyyy-MM-dd"]; } NSDate *endDate = [NSDate dateFromDateStr:_endTime formatStr:@"yyyy-MM-dd"]; NSDate *startDate = [NSDate dateFromDateStr:_startTime formatStr:@"yyyy-MM-dd"]; if ([endDate isEarlierThanDate:startDate]) { [ZCTool showToastOnKeyWindowWith:@"结束时间不能早于开始时间"]; return; } if (_dayRange > 0) { if ([endDate daysAfterDate:startDate] > _dayRange) { NSString *toast = [NSString stringWithFormat:@"开始距结束不能超过%ld天",_dayRange]; kShowToast(toast); return; } } [self dismiss]; if ([self.delegate respondsToSelector:@selector(zcDatePickerViewSelectFinish:endTime:)]) { [self.delegate zcDatePickerViewSelectFinish:startDate endTime:endDate]; } } /** 选中开始时间 */ -(void)startDateSelectAction{ if (_startDateBtn.isSelected) { return; } _startDateBtn.selected = YES; _endDateBtn.selected = NO; NSDate *startDate = [NSDate dateFromDateStr:_startTime formatStr:@"yyyy-MM-dd"]; if (![_datePicker.date isEqualToDate:startDate]) { [_datePicker setDate:startDate animated:NO]; } [self updateSubViews]; } /** 选中结束时间 */ -(void)endDateSelectAction{ if (_endDateBtn.isSelected) { return; } if (_endTime.length == 0) { _endTime = _startTime; } NSDate *endDate = [NSDate dateFromDateStr:_endTime formatStr:@"yyyy-MM-dd"]; if (![_datePicker.date isEqualToDate:endDate]) { [_datePicker setDate:endDate animated:NO]; } _endDateBtn.selected = YES; _startDateBtn.selected = NO; [self updateSubViews]; } /** 刷新视图 */ -(void)updateSubViews{ NSString *startTitle = _startTime.length > 0 ? _startTime : @"开始时间"; [_startDateBtn setTitle:startTitle forState:UIControlStateNormal]; _startLine.backgroundColor = _startDateBtn.isSelected ? kHexColor(@"#EC5428"):KColor_2; NSString *endTitle = _endTime.length > 0 ? _endTime : @"结束时间"; [_endDateBtn setTitle:endTitle forState:UIControlStateNormal]; _endLine.backgroundColor = _endDateBtn.isSelected ? kHexColor(@"#EC5428"):KColor_2; } #pragma mark - public methods - (void)show { [UIView animateWithDuration:0.25 animations:^{ self.top = 20; }]; } - (void)dismiss { [UIView animateWithDuration:0.25 animations:^{ self.top = KScreenHeight; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } -(void)dateChange:(UIDatePicker *)datePicker{ NSDate *date = datePicker.date; if (_startDateBtn.isSelected) { _startTime = [NSString dateStrFromDate:date formatStr:@"yyyy-MM-dd"]; }else{ _endTime = [NSString dateStrFromDate:date formatStr:@"yyyy-MM-dd"]; } [self updateSubViews]; } #pragma mark - getter methods - (UIButton *)cancelBtn { if (!_cancelBtn) { _cancelBtn = [[UIButton alloc] init]; [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; [_cancelBtn setTitleColor:KColor_2 forState:UIControlStateNormal]; [_cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside]; _cancelBtn.titleLabel.font = kFontMedium(16); [_cancelBtn sizeToFit]; } return _cancelBtn; } - (UIButton *)confirmBtn { if (!_confirmBtn) { _confirmBtn = [[UIButton alloc] init]; [_confirmBtn setTitle:@"完成" forState:UIControlStateNormal]; [_confirmBtn setTitleColor:KColor_1 forState:UIControlStateNormal]; [_confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside]; _confirmBtn.titleLabel.font = kFontMedium(16); [_confirmBtn sizeToFit]; } return _confirmBtn; } - (UIView *)naviContainView { if (!_naviContainView) { _naviContainView = [[UIView alloc] init]; _naviContainView.backgroundColor = [UIColor whiteColor]; } return _naviContainView; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.text = @"选择时间"; _titleLabel.textColor = KBlackColor; _titleLabel.font = kFontMedium(20); } return _titleLabel; } - (UIDatePicker *)datePicker{ if (!_datePicker) { _datePicker = [[UIDatePicker alloc] init]; //设置地区: zh-中国 _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"]; //设置日期模式(Displays month, day, and year depending on the locale setting) _datePicker.datePickerMode = UIDatePickerModeCountDownTimer; // 设置当前显示时间 [_datePicker setDate:[NSDate date] animated:YES]; //设置时间格式 _datePicker.datePickerMode = UIDatePickerModeDate; //监听DataPicker的滚动 [_datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged]; } return _datePicker; } - (UIButton *)startDateBtn{ if (!_startDateBtn) { _startDateBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_startDateBtn addTarget:self action:@selector(startDateSelectAction) forControlEvents:UIControlEventTouchUpInside]; [_startDateBtn setTitleColor:kHexColor(@"#EC5428") forState:UIControlStateSelected]; [_startDateBtn setTitleColor:KColor_2 forState:UIControlStateNormal]; } return _startDateBtn; } - (UIButton *)endDateBtn{ if (!_endDateBtn) { _endDateBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_endDateBtn addTarget:self action:@selector(endDateSelectAction) forControlEvents:UIControlEventTouchUpInside]; [_endDateBtn setTitleColor:kHexColor(@"#EC5428") forState:UIControlStateSelected]; [_endDateBtn setTitleColor:KColor_2 forState:UIControlStateNormal]; } return _endDateBtn; } -(UIView *)startLine{ if (_startLine == nil) { _startLine = [[UIView alloc] init]; } return _startLine; } -(UIView *)endLine{ if (_endLine == nil) { _endLine = [[UIView alloc] init]; } return _endLine; } @end