// // PBEditTimeView.m // IphoneBIMe // // Created by zjf on 2020/11/30. // Copyright © 2020 ProBIM. All rights reserved. // #import "PBEditTimeView.h" #import "PBExamineAddModel.h" #import "PBSiteMechanicalModel.h" #define MAXYEAR 2099 #define MINYEAR 2022 @interface PBEditTimeView() @property (nonatomic, strong) UIView *bgView; @property (nonatomic, strong) UIPickerView *pickerView; @property (nonatomic, assign) BOOL isSelsected; @property (nonatomic, weak) UILabel *promptL; @property (nonatomic, copy) NSString *selectStr; @end @implementation PBEditTimeView{ NSMutableArray *_dateArr; //日期存储数组 NSMutableArray *_yearArray; NSMutableArray *_monthArray; NSMutableArray *_dayArray; NSString *_dateFormatter; //记录位置 NSInteger yearIndex; NSInteger monthIndex; NSInteger dayIndex; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0]; [self addSubview:self.bgView]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bgView.bounds; maskLayer.path = maskPath.CGPath; self.bgView.layer.mask = maskLayer; _dateFormatter = @"yyyy-MM-dd"; [self setupUI]; [self defaultConfig]; } return self; } - (void)defaultConfig { NSDate *date = [NSDate date]; NSInteger year = date.year; NSInteger month = date.month; self.selectStr = [NSString stringWithFormat:@"%zd年%zd月%zd日",year, month, date.day]; //设置年月日时分数据 _dateArr = [self setArray:_dateArr]; _yearArray = [self setArray:_yearArray]; _monthArray = [self setArray:_monthArray]; _dayArray = [self setArray:_dayArray]; for (NSInteger i=1; i<13; i++) { NSString *num = [NSString stringWithFormat:@"%zd",i]; [_monthArray addObject:num]; } for (NSInteger i=year-1; i<=year+1; i++) { NSString *num = [NSString stringWithFormat:@"%zd",i]; [_yearArray addObject:num]; } NSInteger startYear; NSInteger startMonth; if (month == 1) { startYear = year - 1; startMonth = 12; }else { startYear = year; startMonth = month - 1; } for (NSInteger i = 0; i < 12; i++) { NSInteger monthIndex = (startMonth + i - 1) % 12; NSInteger monthNum = [_monthArray[monthIndex] integerValue]; NSInteger numDay = [self DaysfromYear:startYear andMonth:monthNum]; for (NSInteger j = 1; j <= numDay; j++) { NSString *timeStr; if (startYear == date.year & monthNum == date.month & j == date.day) { timeStr = @"今天"; }else { timeStr = [NSString stringWithFormat:@"%zd年%zd月%zd日",startYear, monthNum, j]; } [_dateArr addObject:timeStr]; } startYear = startYear + monthNum / 12; } } - (NSMutableArray *)setArray:(id)mutableArray { if (mutableArray) [mutableArray removeAllObjects]; else mutableArray = [NSMutableArray array]; return mutableArray; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CGPoint point = [[touches anyObject]locationInView:self]; CALayer *layer = [self.layer hitTest:point]; if (layer == self.layer) { self.isSelsected = NO; [self hidden]; } } - (void)hidden { if (self.ChooseCompleteBlock) { self.ChooseCompleteBlock(self.isSelsected, self.selectStr); } [UIView animateWithDuration:0.3 animations:^{ self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0]; self.bgView.y = PBScreenHeight; }completion:^(BOOL finished) { for (UIView *cover in PBKeyWindow.subviews) { if ([cover isKindOfClass:[PBEditTimeView class]]) { [cover removeFromSuperview]; } } }]; } - (void)show { [UIView animateWithDuration:0.3 animations:^{ self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5]; self.bgView.y = PBScreenHeight - self.visualViewHeight; }]; } #pragma mark - 懒加载 - (UIView *)bgView { if (_bgView == nil) { _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, PBScreenHeight, PBScreenWidth, PBScreenHeight)]; _bgView.backgroundColor = [UIColor whiteColor]; } return _bgView; } - (void)setVisualViewHeight:(NSInteger)visualViewHeight { _visualViewHeight = visualViewHeight; } - (void)setupUI { UILabel *promptL =[UILabel z_labelWithText:@"" Color:PBColor(40, 58, 79) isBold:YES Font:16]; promptL.textAlignment = NSTextAlignmentCenter; [self.bgView addSubview:promptL]; [promptL mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bgView).offset(12); make.left.equalTo(self.bgView).offset(20); make.right.equalTo(self.bgView).offset(-20); make.height.equalTo(@18); }]; UIView *linView = [[UIView alloc] init]; linView.backgroundColor = PBColor(244, 245, 246); [self.bgView addSubview:linView]; [linView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.bgView).offset(42); make.left.equalTo(self.bgView).offset(25); make.right.equalTo(self.bgView).offset(-25); make.height.equalTo(@1); }]; self.pickerView = [[UIPickerView alloc]init]; self.pickerView.backgroundColor = [UIColor whiteColor]; self.pickerView.delegate = self; self.pickerView.dataSource = self; [self.bgView addSubview:self.pickerView]; [self.pickerView reloadAllComponents];//刷新UIPickerView [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(linView.mas_bottom); make.right.equalTo(self.bgView).offset(-10); make.left.equalTo(self.bgView).offset(10); make.height.equalTo(@193); }]; UIButton *determineBtn = [UIButton z_bgImageButton:[UIImage imageNamed:@"Examine_add_confirm"]]; [self.bgView addSubview:determineBtn]; [determineBtn addTarget:self action:@selector(determineBtnActio) forControlEvents:UIControlEventTouchUpInside]; [determineBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.pickerView.mas_bottom).offset(7); make.left.equalTo(self.bgView).offset(10); make.right.equalTo(self.bgView).offset(-10); make.height.equalTo(@50); }]; self.promptL = promptL; } - (void)determineBtnActio { self.isSelsected = YES; if ([self.selectStr isEqualToString:@"今天"]) { NSDate *date = [NSDate date]; self.selectStr = [NSString stringWithFormat:@"%zd年%zd月%zd日",date.year, date.month, date.day]; } if (self.examineAddModel) { self.examineAddModel.dataStr = self.selectStr.copy; }else if(self.siteMechanicalModel){ self.siteMechanicalModel.MachineDetial_Estimated = self.selectStr.copy; }else { self.fillData = self.selectStr.copy; } [self hidden]; } #pragma mark UIPickerViewDelegate && UIPickerViewDataSource - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return _dateArr.count; } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return 40.f; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ self.selectStr = _dateArr[row]; } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *customLabel = (UILabel *)view; if (!customLabel) { customLabel = [[UILabel alloc] init]; customLabel.textAlignment = NSTextAlignmentCenter; [customLabel setFont:[UIFont systemFontOfSize:20]]; } NSString *title = _dateArr[row]; customLabel.text = title; customLabel.textColor = TitleColor; return customLabel; } //滚动到指定的时间位置 - (void)getNowDate:(NSDate *)date animated:(BOOL)animated { if (!date) { date = [NSDate date]; } NSInteger startYear; NSInteger startMonth; if (date.month == 1) { startYear = date.year - 1; startMonth = 12; }else { startYear = date.year; startMonth = date.month - 1; } NSInteger day = [self DaysfromYear:startYear andMonth:startMonth]; NSInteger index = day + date.day - 1; [self.pickerView selectRow:index inComponent:0 animated:animated]; } //通过年月求每月天数 - (NSInteger)DaysfromYear:(NSInteger)year andMonth:(NSInteger)month { NSInteger num_year = year; NSInteger num_month = month; BOOL isrunNian = num_year%4==0 ? (num_year%100==0? (num_year%400==0?YES:NO):YES):NO; switch (num_month) { case 1:case 3:case 5:case 7:case 8:case 10:case 12:{ // [self setdayArray:31]; return 31; } case 4:case 6:case 9:case 11:{ // [self setdayArray:30]; return 30; } case 2:{ if (isrunNian) { // [self setdayArray:29]; return 29; }else{ // [self setdayArray:28]; return 28; } } default: break; } return 0; } - (void)setExamineAddModel:(PBExamineAddModel *)examineAddModel { _examineAddModel = examineAddModel; self.promptL.text = examineAddModel.title; if(_examineAddModel.dataStr) { NSDate *scrollToDate = [NSDate date:_examineAddModel.dataStr WithFormat:_dateFormatter]; [self getNowDate:scrollToDate animated:NO]; }else { [self getNowDate:nil animated:NO]; } } - (void)setFillData:(NSString *)fillData { _fillData = fillData; self.promptL.text = @"填报日期"; if (fillData) { NSDate *scrollToDate = [NSDate date:_fillData WithFormat:_dateFormatter]; [self getNowDate:scrollToDate animated:NO]; }else { [self getNowDate:nil animated:NO]; } } - (void)setSiteMechanicalModel:(PBSiteMechanicalModel *)siteMechanicalModel { _siteMechanicalModel = siteMechanicalModel; self.promptL.text = @"预计到场日期"; if(_siteMechanicalModel.MachineDetial_Estimated) { NSDate *scrollToDate = [NSDate date:_siteMechanicalModel.MachineDetial_Estimated WithFormat:_dateFormatter]; [self getNowDate:scrollToDate animated:NO]; }else { [self getNowDate:nil animated:NO]; } } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end