// // XHDatePickerView.m // GoldRich // // Created by WindShan on 2017/2/15. // Copyright © 2017年 WindShan. All rights reserved. // #import "XHDatePickerView.h" #import "NSDate+Extension.h" #import "UIView+Extension.h" #import "MBProgressHUD+Add.h" #define MAXYEAR 2050 #define MINYEAR 1970 typedef void(^doneBlock)(NSDate *,NSDate *); @interface XHDatePickerView () { //日期存储数组 NSMutableArray *_yearArray; NSMutableArray *_monthArray; NSMutableArray *_dayArray; NSMutableArray *_hourArray; NSMutableArray *_minuteArray; NSString *_dateFormatter; //记录位置 NSInteger yearIndex; NSInteger monthIndex; NSInteger dayIndex; NSInteger hourIndex; NSInteger minuteIndex; NSInteger preRow; NSDate *_startDate; NSDate *_endDate; } @property (nonatomic, strong) UIView *buttomView; @property (nonatomic, strong) UISegmentedControl *segmentView; @property (nonatomic, strong) UILabel *showYearView; @property (nonatomic, strong) UIButton *doneBtn; @property (nonatomic,strong) UIPickerView *datePicker; @property (nonatomic, retain) NSDate *scrollToDate;//滚到指定日期 @property (nonatomic,strong)doneBlock doneBlock; @end @implementation XHDatePickerView -(void)setupUI { CGFloat offsetH = SCREEN_HEIGHT/2; _buttomView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 367.0, 355.0, 300.0)]; _buttomView.frame = CGRectMake(10.0, 20.0+offsetH, 355.0, 300.0); _buttomView.alpha = 1.000; _buttomView.autoresizesSubviews = YES; _buttomView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; _buttomView.backgroundColor = [UIColor whiteColor]; _buttomView.clearsContextBeforeDrawing = YES; _buttomView.contentMode = UIViewContentModeScaleToFill; _buttomView.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}"); _buttomView.hidden = NO; _buttomView.multipleTouchEnabled = NO; _buttomView.opaque = YES; _buttomView.tag = 0; _buttomView.userInteractionEnabled = YES; [self addSubview:_buttomView]; // UIView *viewiN0 = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 375.0, 667.0)]; // viewiN0.frame = CGRectMake(0.0, 0.0, 375.0, 667.0); // viewiN0.alpha = 1.000; // viewiN0.autoresizesSubviews = YES; // viewiN0.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // viewiN0.backgroundColor = [UIColor clearColor]; // viewiN0.clearsContextBeforeDrawing = YES; // viewiN0.contentMode = UIViewContentModeScaleToFill; // viewiN0.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}"); // viewiN0.hidden = NO; // viewiN0.multipleTouchEnabled = NO; // viewiN0.opaque = YES; // viewiN0.tag = 0; // viewiN0.userInteractionEnabled = YES; // [self addSubview:viewiN0]; self.segmentView = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"开始时间", @"结束时间", nil]]; self.segmentView.frame = CGRectMake((SCREEN_WIDTH-200)/2, 20.0+offsetH+5, 200.0, 29.0); self.segmentView.alpha = 1.000; self.segmentView.autoresizesSubviews = YES; self.segmentView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; self.segmentView.clearsContextBeforeDrawing = YES; self.segmentView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; self.segmentView.contentMode = UIViewContentModeScaleToFill; self.segmentView.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}"); self.segmentView.contentVerticalAlignment = UIControlContentVerticalAlignmentTop; self.segmentView.enabled = YES; self.segmentView.hidden = NO; self.segmentView.highlighted = NO; self.segmentView.momentary = NO; self.segmentView.multipleTouchEnabled = NO; self.segmentView.opaque = NO; self.segmentView.segmentedControlStyle = UISegmentedControlStylePlain; self.segmentView.selected = NO; self.segmentView.selectedSegmentIndex = 0; self.segmentView.tag = 0; self.segmentView.userInteractionEnabled = YES; self.segmentView.selectedSegmentIndex = 0; [self.segmentView addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; [self addSubview:self.segmentView]; _showYearView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 53.0, 355.0, 198.0)]; _showYearView.frame = CGRectMake(0.0, 53.0+offsetH, 355.0, 198.0); _showYearView.adjustsFontSizeToFitWidth = NO; _showYearView.alpha = 1.000; _showYearView.autoresizesSubviews = YES; _showYearView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; _showYearView.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; _showYearView.clearsContextBeforeDrawing = YES; _showYearView.contentMode = UIViewContentModeLeft; _showYearView.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}"); _showYearView.enabled = YES; _showYearView.hidden = NO; _showYearView.lineBreakMode = UILineBreakModeTailTruncation; _showYearView.minimumFontSize = 0.000; _showYearView.multipleTouchEnabled = NO; _showYearView.numberOfLines = 1; _showYearView.opaque = NO; _showYearView.shadowOffset = CGSizeMake(0.0, -1.0); _showYearView.tag = 0; //_showYearView.text = @"2016"; _showYearView.textAlignment = UITextAlignmentCenter; _showYearView.textColor = [UIColor redColor]; _showYearView.userInteractionEnabled = YES; [self addSubview:_showYearView]; _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _doneBtn.frame = CGRectMake(20, 256.0+offsetH, SCREEN_WIDTH-40, 44.0); _doneBtn.adjustsImageWhenDisabled = YES; _doneBtn.adjustsImageWhenHighlighted = YES; _doneBtn.alpha = 1.000; _doneBtn.autoresizesSubviews = YES; _doneBtn.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; _doneBtn.backgroundColor = [UIColor blueColor]; _doneBtn.clearsContextBeforeDrawing = YES; _doneBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; _doneBtn.contentMode = UIViewContentModeScaleToFill; _doneBtn.contentStretch = CGRectFromString(@"{{0, 0}, {1, 1}}"); _doneBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; _doneBtn.enabled = YES; _doneBtn.hidden = NO; _doneBtn.highlighted = NO; _doneBtn.multipleTouchEnabled = NO; _doneBtn.opaque = NO; _doneBtn.reversesTitleShadowWhenHighlighted = NO; _doneBtn.selected = NO; _doneBtn.showsTouchWhenHighlighted = NO; _doneBtn.tag = 0; _doneBtn.titleLabel.lineBreakMode = UILineBreakModeMiddleTruncation; _doneBtn.titleLabel.shadowOffset = CGSizeMake(0.0, 0.0); _doneBtn.userInteractionEnabled = YES; [_doneBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside]; [_doneBtn setTitle:@"确定" forState:UIControlStateNormal]; [self addSubview:_doneBtn]; self.buttomView.layer.cornerRadius = 10; self.buttomView.layer.masksToBounds = YES; self.themeColor = RgbColor(84, 76, 155); self.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); //点击背景是否影藏 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)]; tap.delegate = self; [self addGestureRecognizer:tap]; //self.bottomConstraint.constant = -self.height; self.backgroundColor = RGBA(0, 0, 0, 0); [self layoutIfNeeded]; [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self]; [self.showYearView addSubview:self.datePicker]; } -(void)setDateValue:(NSDate*)start endDate:(NSDate*)end { _startDate = start; _endDate = end; [self getNowDate:_startDate animated:YES]; } -(void)defaultConfig { if (!_scrollToDate) { _scrollToDate = [NSDate date]; } //循环滚动时需要用到 preRow = (self.scrollToDate.year-MINYEAR)*12+self.scrollToDate.month-1; //设置年月日时分数据 _yearArray = [self setArray:_yearArray]; _monthArray = [self setArray:_monthArray]; _dayArray = [self setArray:_dayArray]; _hourArray = [self setArray:_hourArray]; _minuteArray = [self setArray:_minuteArray]; for (int i=0; i<60; i++) { NSString *num = [NSString stringWithFormat:@"%02d",i]; if (00 && [_monthArray[monthIndex] integerValue] < [_monthArray[preRow%12] integerValue]) { yearIndex ++; } else if(preRow-row <12 && preRow-row > 0 && [_monthArray[monthIndex] integerValue] > [_monthArray[ preRow%12] integerValue]) { yearIndex --; }else { NSInteger interval = (row-preRow)/12; yearIndex += interval; } //self.showYearView.text = _yearArray[yearIndex]; preRow = row; } #pragma mark - UIGestureRecognizerDelegate - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { if( [touch.view isDescendantOfView:self.buttomView]) { return NO; } return YES; } #pragma mark - Action -(void)show { [Application.keyWindow addSubview:self]; [UIView animateWithDuration:.3 animations:^ { //self.bottomConstraint.constant = 10; self.backgroundColor = RGBA(0, 0, 0, 0.3); [self layoutIfNeeded]; // 设置当前时间 [self getNowDate:_startDate animated:NO]; }]; } -(void)dismiss { [UIView animateWithDuration:.3 animations:^{ //self.bottomConstraint.constant = -self.height; self.backgroundColor = RGBA(0, 0, 0, 0); [self layoutIfNeeded]; } completion:^(BOOL finished) { [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; [self removeFromSuperview]; }]; } -(void)segmentAction:(UISegmentedControl *)segment { self.dateType = (int)segment.selectedSegmentIndex; BASE_INFO_FUN(self.scrollToDate); switch (self.dateType) { case DateTypeStartDate: { _endDate = [self.scrollToDate dateWithFormatter:_dateFormatter]; [self getNowDate:_startDate animated:NO]; } break; default: { _startDate = [self.scrollToDate dateWithFormatter:_dateFormatter]; [self getNowDate:_endDate animated:NO]; } break; } } #pragma mark - tools //通过年月求每月天数 - (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)getNowDate:(NSDate *)date animated:(BOOL)animated { if (!date) { date = [NSDate date]; } self.scrollToDate = date; [self DaysfromYear:date.year andMonth:date.month]; yearIndex = date.year-MINYEAR; monthIndex = date.month-1; dayIndex = date.day-1; hourIndex = date.hour; minuteIndex = date.minute; //循环滚动时需要用到 preRow = (self.scrollToDate.year-MINYEAR)*12+self.scrollToDate.month-1; NSArray *indexArray; if (self.datePickerStyle == DateStyleShowYearMonthDayHourMinute) indexArray = @[@(yearIndex),@(monthIndex),@(dayIndex),@(hourIndex),@(minuteIndex)]; if (self.datePickerStyle == DateStyleShowYearMonthDay) indexArray = @[@(yearIndex),@(monthIndex),@(dayIndex)]; if (self.datePickerStyle == DateStyleShowMonthDayHourMinute) indexArray = @[@(monthIndex),@(dayIndex),@(hourIndex),@(minuteIndex)]; if (self.datePickerStyle == DateStyleShowMonthDay) indexArray = @[@(monthIndex),@(dayIndex)]; if (self.datePickerStyle == DateStyleShowHourMinute) indexArray = @[@(hourIndex),@(minuteIndex)]; //self.showYearView.text = _yearArray[yearIndex]; [self.datePicker reloadAllComponents]; for (int i=0; i