//
|
// PBTimeChooseView.m
|
// IphoneBIMe
|
//
|
// Created by zjf on 2018/12/26.
|
// Copyright © 2018 ProBIM. All rights reserved.
|
//
|
|
#import "PBTimeChooseView.h"
|
#import "PBExamineAddModel.h"
|
#define MAXYEAR 2099
|
#define MINYEAR 2000
|
|
@interface PBTimeChooseView()<UIPickerViewDelegate,UIPickerViewDataSource>
|
@property (nonatomic, strong) UIView *bgView;
|
@property (nonatomic, strong) UIPickerView *pickerView;
|
|
//@property (nonatomic, strong) NSArray *dataArr;
|
@property (nonatomic, weak) UIView *topView;
|
|
@property (nonatomic, weak) UILabel *leftTitleL;
|
@property (nonatomic, weak) UILabel *rightTitleL;
|
@property (nonatomic, weak) UILabel *leftSelectL;
|
@property (nonatomic, weak) UILabel *rightSelectL;
|
@property (nonatomic, weak) UILabel *promptL;
|
@property (nonatomic, strong) PBExamineAddModel *examineAddModel;
|
@property (nonatomic, assign) BOOL isSelectLeft;
|
@property (nonatomic, assign) BOOL isSelectRight;
|
@property (nonatomic, copy) NSString *selectStr;
|
//@property (nonatomic, strong) NSArray *leftData;
|
//@property (nonatomic, strong) NSArray *rightData;
|
|
@end
|
@implementation PBTimeChooseView{
|
//日期存储数组
|
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];
|
_dateFormatter = @"yyyy-MM-dd";
|
[self setupUI];
|
[self defaultConfig];
|
}
|
return self;
|
}
|
-(void)defaultConfig {
|
//设置年月日时分数据
|
_yearArray = [self setArray:_yearArray];
|
_monthArray = [self setArray:_monthArray];
|
_dayArray = [self setArray:_dayArray];
|
for (int i=1; i<13; i++) {
|
NSString *num = [NSString stringWithFormat:@"%02d",i];
|
[_monthArray addObject:num];
|
}
|
for (NSInteger i=MINYEAR; i<=MAXYEAR; i++) {
|
NSString *num = [NSString stringWithFormat:@"%ld",(long)i];
|
[_yearArray addObject:num];
|
}
|
}
|
- (NSMutableArray *)setArray:(id)mutableArray {
|
if (mutableArray)
|
[mutableArray removeAllObjects];
|
else
|
mutableArray = [NSMutableArray array];
|
return mutableArray;
|
}
|
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
|
CGPoint point = [[touches anyObject]locationInView:self];
|
CALayer *layer = [self.layer hitTest:point];
|
if (layer == self.layer) {
|
[self hidden];
|
}
|
}
|
- (void)hidden {
|
if (self.ChooseCompleteBlock) {
|
self.ChooseCompleteBlock(_isSelectLeft, _isSelectRight);
|
}
|
[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:[PBTimeChooseView 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 {
|
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bgView.width / 2, 3)];
|
topView.backgroundColor = WarningColor;
|
[self.bgView addSubview:topView];
|
|
UIView *linView = [[UIView alloc] init];
|
linView.backgroundColor = [UIColor z_colorWithR:242 G:242 B:242];
|
[self.bgView addSubview:linView];
|
[linView mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(self.bgView).offset(33);
|
make.centerX.equalTo(self.bgView);
|
make.width.equalTo(@1);
|
make.height.equalTo(@55);
|
}];
|
UIButton *leftBtn = [[UIButton alloc] init];
|
[leftBtn addTarget:self action:@selector(statusBtnAction) forControlEvents:UIControlEventTouchUpInside];
|
[self.bgView addSubview:leftBtn];
|
[leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(topView.mas_bottom);
|
make.left.equalTo(self.bgView);
|
make.right.equalTo(linView.mas_left);
|
make.height.equalTo(@129);
|
}];
|
|
UIButton *rightBtn = [[UIButton alloc] init];
|
[rightBtn addTarget:self action:@selector(typeBtnAction) forControlEvents:UIControlEventTouchUpInside];
|
[self.bgView addSubview:rightBtn];
|
[rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(topView.mas_bottom);
|
make.right.equalTo(self.bgView);
|
make.left.equalTo(linView.mas_right);
|
make.height.equalTo(@129);
|
}];
|
|
UILabel *leftTieleL = [UILabel z_labelWithText:@"状态" Color:PromptColor isBold:NO Font:DescFontSize];
|
[self.bgView addSubview:leftTieleL];
|
[leftTieleL mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(leftBtn).offset(31);
|
make.centerX.equalTo(leftBtn);
|
make.height.equalTo(@20);
|
}];
|
|
UILabel *rightTieleL = [UILabel z_labelWithText:@"类型" Color:PromptColor isBold:NO Font:DescFontSize];
|
[self.bgView addSubview:rightTieleL];
|
[rightTieleL mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(rightBtn).offset(31);
|
make.centerX.equalTo(rightBtn);
|
make.height.equalTo(@20);
|
}];
|
|
UILabel *leftSelectL = [UILabel z_labelWithText:@"请选择" Color:WarningColor isBold:NO Font:MarkedFontSize];
|
leftSelectL.textAlignment = NSTextAlignmentCenter;
|
[self.bgView addSubview:leftSelectL];
|
[leftSelectL mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(leftTieleL.mas_bottom).offset(14);
|
make.left.equalTo(leftBtn).offset(16);
|
make.right.equalTo(leftBtn).offset(-16);
|
make.height.equalTo(@25);
|
}];
|
|
UILabel *rightSelectL = [UILabel z_labelWithText:@"请选择" Color:IgnoreColor isBold:NO Font:MarkedFontSize];
|
rightSelectL.textAlignment = NSTextAlignmentCenter;
|
[self.bgView addSubview:rightSelectL];
|
[rightSelectL mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(rightTieleL.mas_bottom).offset(14);
|
make.left.equalTo(rightBtn).offset(16);
|
make.right.equalTo(rightBtn).offset(-16);
|
make.height.equalTo(@25);
|
}];
|
|
UIView *lin1View = [[UIView alloc] init];
|
lin1View.backgroundColor = [UIColor z_colorWithR:242 G:242 B:242];
|
[self.bgView addSubview:lin1View];
|
[lin1View mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(leftBtn.mas_bottom);
|
make.left.right.equalTo(self.bgView);
|
make.height.equalTo(@1);
|
}];
|
|
UILabel *promptL =[UILabel z_labelWithText:@"请选择状态" Color:PromptColor isBold:NO Font:MarkedFontSize];
|
promptL.textAlignment = NSTextAlignmentCenter;
|
[self.bgView addSubview:promptL];
|
[promptL mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(lin1View).offset(15);
|
make.size.mas_equalTo(CGSizeMake(130, 25));
|
make.centerX.equalTo(self.bgView);
|
}];
|
|
UIButton *determineBtn = [UIButton z_textButton:@"确定" fontSize:MarkedFontSize normalColor:WarningColor];
|
[self.bgView addSubview:determineBtn];
|
[determineBtn addTarget:self action:@selector(determineBtnActio) forControlEvents:UIControlEventTouchUpInside];
|
[determineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.centerY.equalTo(promptL);
|
make.right.equalTo(self.bgView).offset(-16);
|
make.size.mas_equalTo(CGSizeMake(52, 50));
|
}];
|
|
UIView *lin2View = [[UIView alloc] init];
|
lin2View.backgroundColor = [UIColor z_colorWithR:242 G:242 B:242];
|
[self.bgView addSubview:lin2View];
|
[lin2View mas_makeConstraints:^(MASConstraintMaker *make) {
|
make.top.equalTo(promptL.mas_bottom).offset(13);
|
make.left.right.equalTo(self.bgView);
|
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(lin2View.mas_bottom);
|
make.right.equalTo(self.bgView).offset(-44);
|
make.left.equalTo(self.bgView).offset(44);
|
make.height.equalTo(@282);
|
}];
|
self.topView = topView;
|
self.leftTitleL = leftTieleL;
|
self.rightTitleL = rightTieleL;
|
self.leftSelectL = leftSelectL;
|
self.rightSelectL = rightSelectL;
|
self.promptL = promptL;
|
}
|
|
- (void)determineBtnActio {
|
if (self.index == 0) {
|
self.isSelectLeft = YES;
|
self.leftExamineAddModel.dataStr = self.selectStr.copy;
|
self.leftSelectL.text = self.leftExamineAddModel.dataStr;
|
if(self.rightExamineAddModel.dataStr) {
|
[self hidden];
|
}else {
|
self.index = 1;
|
}
|
}else {
|
self.isSelectRight = YES;
|
self.rightExamineAddModel.dataStr = self.selectStr.copy;
|
self.rightSelectL.text = self.rightExamineAddModel.dataStr;
|
if(self.leftExamineAddModel.dataStr) {
|
[self hidden];
|
}else {
|
self.index = 0;
|
}
|
}
|
}
|
- (void)statusBtnAction {
|
self.index = 0;
|
}
|
|
- (void)typeBtnAction {
|
if (self.rightExamineAddModel.roleType != CREATOR) {
|
return;
|
}
|
self.index = 1;
|
}
|
|
#pragma mark UIPickerViewDelegate && UIPickerViewDataSource
|
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
|
return 3;
|
}
|
|
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
|
NSArray *numberArr = [self getNumberOfRowsInComponent];
|
return [numberArr[component] integerValue];
|
}
|
|
- (NSArray *)getNumberOfRowsInComponent {
|
NSInteger yearNum = _yearArray.count;
|
NSInteger monthNum = _monthArray.count;
|
NSInteger dayNum = [self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
|
return @[@(yearNum),@(monthNum),@(dayNum)];
|
}
|
|
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
|
return 60.f;
|
}
|
|
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
|
if (component == 0) {
|
yearIndex = row;
|
}
|
if (component == 1) {
|
monthIndex = row;
|
}
|
if (component == 2) {
|
dayIndex = row;
|
}
|
if (component == 0 || component == 1){
|
[self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
|
if (_dayArray.count-1<dayIndex) {
|
dayIndex = _dayArray.count-1;
|
}
|
[pickerView reloadComponent:2];
|
}
|
|
self.selectStr = [NSString stringWithFormat:@"%@-%@-%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex]];
|
}
|
|
- (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;
|
if (component==0) {
|
title = _yearArray[row];
|
}else if (component==1) {
|
title = _monthArray[row];
|
}else if (component==2) {
|
title = _dayArray[row];
|
}
|
|
customLabel.text = title;
|
customLabel.textColor = TitleColor;
|
return customLabel;
|
}
|
- (void)setIndex:(NSInteger)index {
|
_index = index;
|
if (index == 0) {
|
[UIView animateWithDuration:0.2 animations:^{
|
self.topView.x = 0;
|
}];
|
self.leftSelectL.textColor = WarningColor;
|
if (self.rightExamineAddModel.dataStr) {
|
self.rightSelectL.textColor = TitleColor;
|
}else {
|
self.rightSelectL.textColor = IgnoreColor;
|
}
|
self.promptL.text = self.leftExamineAddModel.prompt;
|
if(_examineAddModel.dataStr) {
|
NSDate *scrollToDate = [NSDate date:_examineAddModel.dataStr WithFormat:_dateFormatter];
|
[self getNowDate:scrollToDate animated:NO];
|
}else {
|
[self getNowDate:nil animated:NO];
|
}
|
}else {
|
[UIView animateWithDuration:0.2 animations:^{
|
self.topView.x = self.bgView.width / 2;
|
}];
|
self.rightSelectL.textColor = WarningColor;
|
if (self.leftExamineAddModel.dataStr) {
|
self.leftSelectL.textColor = TitleColor;
|
}else {
|
self.leftSelectL.textColor = IgnoreColor;
|
}
|
self.promptL.text = self.rightExamineAddModel.prompt;
|
if(_examineAddModel.dataStr) {
|
NSDate *scrollToDate = [NSDate date:_examineAddModel.dataStr WithFormat:_dateFormatter];
|
[self getNowDate:scrollToDate animated:NO];
|
}else {
|
[self getNowDate:nil animated:NO];
|
}
|
}
|
}
|
//滚动到指定的时间位置
|
- (void)getNowDate:(NSDate *)date animated:(BOOL)animated {
|
if (!date) {
|
date = [NSDate date];
|
}
|
[self DaysfromYear:date.year andMonth:date.month];
|
self.selectStr = [NSString stringWithFormat:@"%zd-%02zd-%02zd",date.year, date.month, date.day];
|
yearIndex = date.year-MINYEAR;
|
monthIndex = date.month-1;
|
dayIndex = date.day-1;
|
NSArray *indexArray = @[@(yearIndex),@(monthIndex),@(dayIndex)];
|
[self.pickerView reloadAllComponents];
|
for (int i=0; i<indexArray.count; i++) {
|
[self.pickerView selectRow:[indexArray[i] integerValue] inComponent:i 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)setdayArray:(NSInteger)num {
|
[_dayArray removeAllObjects];
|
for (int i=1; i<=num; i++) {
|
[_dayArray addObject:[NSString stringWithFormat:@"%02d",i]];
|
}
|
}
|
|
- (void)setLeftExamineAddModel:(PBExamineAddModel *)leftExamineAddModel {
|
_leftExamineAddModel = leftExamineAddModel;
|
_leftTitleL.text = leftExamineAddModel.title;
|
if (leftExamineAddModel.dataStr) {
|
_leftSelectL.text = leftExamineAddModel.dataStr;
|
}else {
|
_leftSelectL.text = @"请选择";
|
}
|
}
|
|
- (void)setRightExamineAddModel:(PBExamineAddModel *)rightExamineAddModel {
|
_rightExamineAddModel = rightExamineAddModel;
|
_rightTitleL.text = rightExamineAddModel.title;
|
if (rightExamineAddModel.dataStr) {
|
_rightSelectL.text = rightExamineAddModel.dataStr;
|
}else {
|
_rightSelectL.text = @"请选择";
|
}
|
}
|
- (void)setIsAddRecord:(BOOL)isAddRecord {
|
_isAddRecord = isAddRecord;
|
}
|
@end
|