//
|
// 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 ()<UIPickerViewDelegate,UIPickerViewDataSource,UIGestureRecognizerDelegate>
|
{
|
//日期存储数组
|
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 (0<i && i<=12)
|
[_monthArray addObject:num];
|
if (i<24)
|
[_hourArray addObject:num];
|
[_minuteArray addObject:num];
|
}
|
|
for (NSInteger i=MINYEAR; i<MAXYEAR; i++)
|
{
|
NSString *num = [NSString stringWithFormat:@"%ld",(long)i];
|
[_yearArray addObject:num];
|
}
|
|
//最大最小限制
|
if (!self.maxLimitDate)
|
{
|
self.maxLimitDate = [NSDate date:@"2049-12-31 23:59" WithFormat:@"yyyy-MM-dd HH:mm"];
|
}
|
|
//最小限制
|
if (!self.minLimitDate) {
|
self.minLimitDate = [NSDate dateWithTimeIntervalSince1970:0];
|
}
|
}
|
|
-(void)addLabelWithName:(NSArray *)nameArr
|
{
|
for (id subView in self.showYearView.subviews)
|
{
|
if ([subView isKindOfClass:[UILabel class]])
|
{
|
[subView removeFromSuperview];
|
}
|
}
|
for (int i=0; i<nameArr.count; i++)
|
{
|
CGFloat labelX = self.datePicker.frame.size.width/(nameArr.count*2)+18+self.datePicker.frame.size.width/nameArr.count*i;
|
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(labelX, self.showYearView.frame.size.height/2-8, 15, 15)];
|
label.text = nameArr[i];
|
label.textAlignment = NSTextAlignmentCenter;
|
label.font = [UIFont systemFontOfSize:14];
|
label.textColor = self.themeColor;
|
label.backgroundColor = [UIColor clearColor];
|
[self.showYearView addSubview:label];
|
}
|
}
|
|
|
- (NSMutableArray *)setArray:(id)mutableArray
|
{
|
if (mutableArray)
|
[mutableArray removeAllObjects];
|
else
|
mutableArray = [NSMutableArray array];
|
return mutableArray;
|
}
|
|
- (void)doneAction
|
{
|
switch (self.dateType)
|
{
|
case DateTypeStartDate:
|
_startDate = [self.scrollToDate dateWithFormatter:_dateFormatter];
|
break;
|
|
default:
|
_endDate = [self.scrollToDate dateWithFormatter:_dateFormatter];
|
break;
|
}
|
|
if([_startDate timeIntervalSinceDate:_endDate] < 0.0)
|
{
|
self.doneBlock(_startDate,_endDate);
|
[self dismiss];
|
}
|
else
|
{
|
[MBProgressHUD showTipsMsg:@"结束时间必须大于开始时间" toView:self];
|
}
|
}
|
|
-(instancetype)initWithCompleteBlock:(void(^)(NSDate *,NSDate *))completeBlock
|
{
|
self = [super init];
|
if (self)
|
{
|
//self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] lastObject];
|
|
_dateFormatter = @"yyyy-MM-dd HH:mm";
|
[self setupUI];
|
[self defaultConfig];
|
|
if (completeBlock)
|
{
|
self.doneBlock = ^(NSDate *startDate,NSDate *endDate) {
|
completeBlock(startDate,endDate);
|
};
|
}
|
}
|
return self;
|
}
|
|
#pragma mark - UIPickerViewDelegate,UIPickerViewDataSource
|
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
|
{
|
switch (self.datePickerStyle) {
|
case DateStyleShowYearMonthDayHourMinute:
|
[self addLabelWithName:@[@"年",@"月",@"日",@"时",@"分"]];
|
return 5;
|
case DateStyleShowYearMonthDay:
|
[self addLabelWithName:@[@"年",@"月",@"日"]];
|
return 3;
|
case DateStyleShowMonthDayHourMinute:
|
[self addLabelWithName:@[@"月",@"日",@"时",@"分"]];
|
return 4;
|
case DateStyleShowMonthDay:
|
[self addLabelWithName:@[@"月",@"日"]];
|
return 2;
|
case DateStyleShowHourMinute:
|
[self addLabelWithName:@[@"时",@"分"]];
|
return 2;
|
default:
|
return 0;
|
}
|
}
|
|
- (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]];
|
NSInteger hourNum = _hourArray.count;
|
NSInteger minuteNUm = _minuteArray.count;
|
|
NSInteger timeInterval = MAXYEAR - MINYEAR;
|
|
switch (self.datePickerStyle) {
|
case DateStyleShowYearMonthDayHourMinute:
|
return @[@(yearNum),@(monthNum),@(dayNum),@(hourNum),@(minuteNUm)];
|
break;
|
case DateStyleShowMonthDayHourMinute:
|
return @[@(monthNum*timeInterval),@(dayNum),@(hourNum),@(minuteNUm)];
|
break;
|
case DateStyleShowYearMonthDay:
|
return @[@(yearNum),@(monthNum),@(dayNum)];
|
break;
|
case DateStyleShowMonthDay:
|
return @[@(monthNum*timeInterval),@(dayNum),@(hourNum)];
|
break;
|
case DateStyleShowHourMinute:
|
return @[@(hourNum),@(minuteNUm)];
|
break;
|
default:
|
return @[];
|
break;
|
}
|
|
}
|
|
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
|
return 40;
|
}
|
|
|
-(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:17]];
|
}
|
NSString *title;
|
|
switch (self.datePickerStyle) {
|
case DateStyleShowYearMonthDayHourMinute:
|
if (component==0) {
|
title = _yearArray[row];
|
}
|
if (component==1) {
|
title = _monthArray[row];
|
}
|
if (component==2) {
|
title = _dayArray[row];
|
}
|
if (component==3) {
|
title = _hourArray[row];
|
}
|
if (component==4) {
|
title = _minuteArray[row];
|
}
|
break;
|
case DateStyleShowYearMonthDay:
|
if (component==0) {
|
title = _yearArray[row];
|
}
|
if (component==1) {
|
title = _monthArray[row];
|
}
|
if (component==2) {
|
title = _dayArray[row];
|
}
|
break;
|
case DateStyleShowMonthDayHourMinute:
|
if (component==0) {
|
title = _monthArray[row%12];
|
}
|
if (component==1) {
|
title = _dayArray[row];
|
}
|
if (component==2) {
|
title = _hourArray[row];
|
}
|
if (component==3) {
|
title = _minuteArray[row];
|
}
|
break;
|
case DateStyleShowMonthDay:
|
if (component==0) {
|
title = _monthArray[row%12];
|
}
|
if (component==1) {
|
title = _dayArray[row];
|
}
|
break;
|
case DateStyleShowHourMinute:
|
if (component==0) {
|
title = _hourArray[row];
|
}
|
if (component==1) {
|
title = _minuteArray[row];
|
}
|
break;
|
default:
|
title = @"";
|
break;
|
}
|
|
customLabel.text = title;
|
customLabel.textColor = [UIColor blackColor];
|
return customLabel;
|
|
}
|
|
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
|
{
|
switch (self.datePickerStyle)
|
{
|
case DateStyleShowYearMonthDayHourMinute:
|
{
|
|
if (component == 0) {
|
yearIndex = row;
|
|
//self.showYearView.text =_yearArray[yearIndex];
|
}
|
if (component == 1) {
|
monthIndex = row;
|
}
|
if (component == 2) {
|
dayIndex = row;
|
}
|
if (component == 3) {
|
hourIndex = row;
|
}
|
if (component == 4) {
|
minuteIndex = row;
|
}
|
if (component == 0 || component == 1){
|
[self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
|
if (_dayArray.count-1<dayIndex) {
|
dayIndex = _dayArray.count-1;
|
}
|
|
}
|
}
|
break;
|
|
|
case DateStyleShowYearMonthDay:{
|
|
if (component == 0)
|
{
|
yearIndex = row;
|
//self.showYearView.text =_yearArray[yearIndex];
|
}
|
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;
|
}
|
}
|
}
|
break;
|
|
|
case DateStyleShowMonthDayHourMinute:{
|
|
|
if (component == 1) {
|
dayIndex = row;
|
}
|
if (component == 2) {
|
hourIndex = row;
|
}
|
if (component == 3) {
|
minuteIndex = row;
|
}
|
|
if (component == 0) {
|
|
[self yearChange:row];
|
|
if (_dayArray.count-1<dayIndex) {
|
dayIndex = _dayArray.count-1;
|
}
|
}
|
[self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
|
|
}
|
break;
|
|
case DateStyleShowMonthDay:{
|
if (component == 1) {
|
dayIndex = row;
|
}
|
if (component == 0) {
|
|
[self yearChange:row];
|
|
if (_dayArray.count-1<dayIndex) {
|
dayIndex = _dayArray.count-1;
|
}
|
}
|
[self DaysfromYear:[_yearArray[yearIndex] integerValue] andMonth:[_monthArray[monthIndex] integerValue]];
|
}
|
break;
|
|
case DateStyleShowHourMinute:{
|
if (component == 0) {
|
hourIndex = row;
|
}
|
if (component == 1) {
|
minuteIndex = row;
|
}
|
}
|
break;
|
|
default:
|
break;
|
}
|
|
[pickerView reloadAllComponents];
|
|
NSString *dateStr = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",_yearArray[yearIndex],_monthArray[monthIndex],_dayArray[dayIndex],_hourArray[hourIndex],_minuteArray[minuteIndex]];
|
|
self.scrollToDate = [[NSDate date:dateStr WithFormat:@"yyyy-MM-dd HH:mm"] dateWithFormatter:_dateFormatter];
|
|
if ([self.scrollToDate compare:self.minLimitDate] == NSOrderedAscending)
|
{
|
self.scrollToDate = self.minLimitDate;
|
[self getNowDate:self.minLimitDate animated:YES];
|
}
|
else if ([self.scrollToDate compare:self.maxLimitDate] == NSOrderedDescending)
|
{
|
self.scrollToDate = self.maxLimitDate;
|
[self getNowDate:self.maxLimitDate animated:YES];
|
}
|
|
switch (self.dateType)
|
{
|
case DateTypeStartDate:
|
_startDate = self.scrollToDate;
|
break;
|
|
default:
|
_endDate = self.scrollToDate;
|
break;
|
}
|
|
|
NSLog(@"%@",self.scrollToDate);
|
}
|
|
-(void)yearChange:(NSInteger)row {
|
|
monthIndex = row%12;
|
|
//年份状态变化
|
if (row-preRow <12 && row-preRow>0 && [_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<indexArray.count; i++)
|
{
|
if ((self.datePickerStyle == DateStyleShowMonthDayHourMinute || self.datePickerStyle == DateStyleShowMonthDay)&& i==0) {
|
NSInteger mIndex = [indexArray[i] integerValue]+(12*(self.scrollToDate.year - MINYEAR));
|
[self.datePicker selectRow:mIndex inComponent:i animated:animated];
|
} else {
|
[self.datePicker selectRow:[indexArray[i] integerValue] inComponent:i animated:animated];
|
}
|
|
}
|
}
|
|
|
#pragma mark - getter / setter
|
-(UIPickerView *)datePicker
|
{
|
if (!_datePicker)
|
{
|
[self.showYearView layoutIfNeeded];
|
_datePicker = [[UIPickerView alloc] initWithFrame:self.showYearView.bounds];
|
_datePicker.showsSelectionIndicator = YES;
|
_datePicker.delegate = self;
|
_datePicker.dataSource = self;
|
}
|
return _datePicker;
|
}
|
|
-(void)setMinLimitDate:(NSDate *)minLimitDate
|
{
|
_minLimitDate = minLimitDate;
|
if ([_scrollToDate compare:self.minLimitDate] == NSOrderedAscending)
|
{
|
_scrollToDate = self.minLimitDate;
|
}
|
|
[self getNowDate:self.scrollToDate animated:NO];
|
}
|
|
-(void)setThemeColor:(UIColor *)themeColor
|
{
|
_themeColor = themeColor;
|
self.segmentView.tintColor = themeColor;
|
self.doneBtn.backgroundColor = themeColor;
|
}
|
|
-(void)setDateType:(XHDateType)dateType
|
{
|
_dateType = dateType;
|
switch (dateType)
|
{
|
case DateTypeStartDate:
|
self.segmentView.selectedSegmentIndex = 0;
|
break;
|
|
default:
|
self.segmentView.selectedSegmentIndex = 1;
|
break;
|
}
|
}
|
-(void)setDatePickerStyle:(XHDateStyle)datePickerStyle
|
{
|
_datePickerStyle = datePickerStyle;
|
switch (datePickerStyle)
|
{
|
break;
|
case DateStyleShowYearMonthDay:
|
case DateStyleShowMonthDay:
|
_dateFormatter = @"yyyy-MM-dd";
|
break;
|
|
default:
|
break;
|
}
|
|
[self.datePicker reloadAllComponents];
|
}
|
|
/*
|
// Only override drawRect: if you perform custom drawing.
|
// An empty implementation adversely affects performance during animation.
|
- (void)drawRect:(CGRect)rect {
|
// Drawing code
|
}
|
*/
|
|
@end
|