单军华
2018-03-28 f99cf1d5cc50407394501853be06cb39f38a092c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
//
//  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