PGDatePicker+DateHourMinuteSecond.m
5.78 KB
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
//
// PGDatePicker+DateHourMinuteSecond.m
// Demo
//
// Created by piggybear on 2018/3/18.
// Copyright © 2018年 piggybear. All rights reserved.
//
#import "PGDatePicker+DateHourMinuteSecond.h"
#import "PGDatePickerHeader.h"
#import "PGDatePicker+Common.h"
#import "PGDatePicker+Logic.h"
@implementation PGDatePicker (DateHourMinuteSecond)
- (void)dateHourMinuteSecond_setupSelectedDate {
NSString *yearString = [self.pickerView textOfSelectedRowInComponent:0];
yearString = [yearString componentsSeparatedByString:self.yearString].firstObject;
self.selectedComponents.year = [yearString integerValue];
NSString *monthString = [self.pickerView textOfSelectedRowInComponent:1];
monthString = [monthString componentsSeparatedByString:self.monthString].firstObject;
self.selectedComponents.month = [monthString integerValue];
NSString *dayString = [self.pickerView textOfSelectedRowInComponent:2];
dayString = [dayString componentsSeparatedByString:self.dayString].firstObject;
self.selectedComponents.day = [dayString integerValue];
NSString *hourString = [self.pickerView textOfSelectedRowInComponent:3];
hourString = [hourString componentsSeparatedByString:self.hourString].firstObject;
self.selectedComponents.hour = [hourString integerValue];
NSString *minuteString = [self.pickerView textOfSelectedRowInComponent:4];
minuteString = [minuteString componentsSeparatedByString:self.minuteString].firstObject;
self.selectedComponents.minute = [minuteString integerValue];
NSString *secondString = [self.pickerView textOfSelectedRowInComponent:5];
secondString = [secondString componentsSeparatedByString:self.secondString].firstObject;
self.selectedComponents.second = [secondString integerValue];
}
- (void)dateHourMinuteSecond_setDateWithComponents:(NSDateComponents *)components animated:(BOOL)animated {
BOOL tf = false;
if (components.year > self.maximumComponents.year) {
components.year = self.maximumComponents.year;
tf = true;
}else if (components.year < self.minimumComponents.year) {
components.year = self.minimumComponents.year;
tf = true;
}
NSInteger row = components.year - self.minimumComponents.year;
[self.pickerView selectRow:row inComponent:0 animated:animated];
if (tf) {
return;
}
{
NSInteger row = 0;
NSString *string = [NSString stringWithFormat:@"%ld", components.month];
BOOL isExist = [self.monthList containsObject:string];
if (isExist) {
row = [self.monthList indexOfObject:string];
}
[self.pickerView selectRow:row inComponent:1 animated:animated];
}
{
NSInteger row = 0;
NSString *string = [NSString stringWithFormat:@"%ld", components.day];
BOOL isExist = [self.dayList containsObject:string];
if (isExist) {
row =[self.dayList indexOfObject:string];
}
[self.pickerView selectRow:row inComponent:2 animated:animated];
}
{
NSInteger row = 0;
NSString *string = [NSString stringWithFormat:@"%ld", components.hour];
if (components.hour < 10) {
string = [NSString stringWithFormat:@"0%ld", components.hour];
}
BOOL isExist = [self.hourList containsObject:string];
if (isExist) {
row = [self.hourList indexOfObject:string];
}
[self.pickerView selectRow:row inComponent:3 animated:animated];
}
{
NSInteger row = 0;
NSString *string = [NSString stringWithFormat:@"%ld", components.minute];
if (components.minute < 10) {
string = [NSString stringWithFormat:@"0%ld", components.minute];
}
BOOL isExist = [self.minuteList containsObject:string];
if (isExist) {
row = [self.minuteList indexOfObject:string];
}
[self.pickerView selectRow:row inComponent:4 animated:animated];
}
{
NSString *string = [NSString stringWithFormat:@"%ld", components.second];
if (components.second < 10) {
string = [NSString stringWithFormat:@"0%ld", components.second];
}
NSInteger row = 0;
BOOL isExist = [self.secondList containsObject:string];
if (isExist) {
row = [self.secondList indexOfObject:string];
}
[self.pickerView selectRow:row inComponent:5 animated:animated];
}
}
- (void)dateHourMinuteSecond_didSelectWithComponent:(NSInteger)component {
NSDateComponents *dateComponents = [self.calendar components:self.unitFlags fromDate:[NSDate date]];
NSString *str = [[self.pickerView textOfSelectedRowInComponent:0] componentsSeparatedByString:self.yearString].firstObject;
dateComponents.year = [str integerValue];
if (component == 0) {
BOOL refresh = [self setMonthListWithComponents:dateComponents refresh:true];
[self.pickerView reloadComponent:1 refresh:refresh];
}
if (component == 0 || component == 1) {
BOOL refresh = [self setDayListWithComponent:component dateComponents:dateComponents refresh:true];
[self.pickerView reloadComponent:2 refresh:refresh];
}
if (component == 0 || component == 1 || component == 2) {
BOOL refresh = [self setHourList2WithDateComponents:dateComponents refresh:true];
[self.pickerView reloadComponent:3 refresh:refresh];
}
if (component == 0 || component == 1 || component == 2 || component == 3) {
BOOL refresh = [self setMinuteList2WithComponent:component dateComponents:dateComponents refresh:true];
[self.pickerView reloadComponent:4 refresh:refresh];
}
if (component != 5) {
BOOL refresh = [self setSecondList2WithComponent:component dateComponents:dateComponents refresh:true];
[self.pickerView reloadComponent:5 refresh:refresh];
}
}
@end