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
//
// FinishTimeView.m
// XFFruit
//
// Created by 陈俊俊 on 15/11/2.
// Copyright © 2015年 Xummer. All rights reserved.
//
#import "FinishTimeView.h"
// 起始年数
#define kStartYear 1980
// 总年数
#define kYearCount 200
#define DateViewHeight 246
@interface FinishTimeView ()
{
UIView *_bgView;
}
// 定义数据属性
// 1. 年数组
@property (strong, nonatomic) NSArray *yearList;
// 2. 月数组
@property (strong, nonatomic) NSArray *monthList;
// 3. 日数组
@property (strong, nonatomic) NSMutableArray *dayList;
// 时
@property (strong,nonatomic) NSArray *hourList;
// 分
@property (strong,nonatomic) NSArray *minuteList;
// 4. 选中的年数
@property (assign, nonatomic) NSInteger selectedYear;
// 5. 选中的月数
@property (assign, nonatomic) NSInteger selectedMonth;
@property (assign,nonatomic) NSInteger currentYear;
@property (assign,nonatomic) NSInteger currentMonth;
@property (assign,nonatomic) NSInteger currentDay;
@property (assign,nonatomic) NSInteger currentHour;
@property (assign,nonatomic) NSInteger currentMinute;
@property (nonatomic,strong) UIPickerView *pickerView;
@property (nonatomic,strong) NSString *dateStr;
@end
@implementation FinishTimeView
- (instancetype)initWithFrame:(CGRect)frame withDate:(NSString *)dateStr
{
self = [super initWithFrame:frame];
if (self) {
self.dateStr = dateStr;
[self buildLayout];
}
self.backgroundColor = [UIColor whiteColor];
return self;
}
- (void)buildLayout
{
_bgView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - DateViewHeight, ScreenSize.width, DateViewHeight)];
_bgView.backgroundColor = RGBA(239, 239, 239 ,1);
[self addSubview:_bgView];
UIButton *okBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(ScreenSize.width - 62, 0, 62, 28) target:self sel:@selector(okClick) tag:0 image:nil title:@"确定" titleColor:[UIColor blackColor] isCorner:NO corner:0 bgColor:RGBA(239, 239, 239 ,1)];
[_bgView addSubview:okBtn];
UIButton *cancelBtn = [IBTCustomButtom creatButtonWithFrame:CGRectMake(0, 0, 62, 28) target:self sel:@selector(cancelClick) tag:0 image:nil title:@"取消" titleColor:[UIColor blackColor] isCorner:NO corner:0 bgColor:RGBA(239, 239, 239 ,1)];
[_bgView addSubview:cancelBtn];
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 30,ScreenSize.width, DateViewHeight - 30)];
pickerView.backgroundColor = [UIColor whiteColor];
// 指定数据源
[pickerView setDataSource:self];
// 指定代理
[pickerView setDelegate:self];
// PickerView默认是没有选中标示的
[pickerView setShowsSelectionIndicator:YES];
self.pickerView = pickerView;
[_bgView addSubview:pickerView];
CLog(@"====%@",self.dateStr);
self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
self.currentDay = [[self.dateStr substringWithRange:NSMakeRange(8,2)] intValue];
self.currentHour = [[self.dateStr substringWithRange:NSMakeRange(11,2)] intValue];
self.currentMinute = [[self.dateStr substringWithRange:NSMakeRange(14,2)] intValue];
// 3. 初始化数据
// 1) 年数据
NSMutableArray *yearArray = [NSMutableArray arrayWithCapacity:kYearCount];
for (NSInteger i = 0; i < kYearCount; i++) {
NSString *str = [NSString stringWithFormat:@"%d年", i + kStartYear];
[yearArray addObject:str];
}
self.yearList = yearArray;
// 2) 月数据
NSMutableArray *monthArray = [NSMutableArray arrayWithCapacity:12];
for (NSInteger i = 1; i <= 12; i++) {
NSString *str = [NSString stringWithFormat:@"%d月", i];
[monthArray addObject:str];
}
self.monthList = monthArray;
// 时
NSMutableArray *hourArray = [NSMutableArray arrayWithCapacity:24];
for (NSInteger i = 0; i < 24; i++) {
NSString *str = [NSString stringWithFormat:@"%d时",i];
[hourArray addObject:str];
}
self.hourList = hourArray;
// 分
NSMutableArray *minuteArray = [NSMutableArray arrayWithCapacity:60];
for (NSInteger i = 0; i < 60; i++) {
NSString *str = [NSString stringWithFormat:@"%d分",i];
[minuteArray addObject:str];
}
self.minuteList = minuteArray;
// 初始化选中年、月
self.currentYear = [[self.dateStr substringWithRange:NSMakeRange(0,4)] intValue];
self.currentMonth = [[self.dateStr substringWithRange:NSMakeRange(5,2)] intValue];
self.currentDay = [[self.dateStr substringWithRange:NSMakeRange(8,2)] intValue];
self.currentHour = [[self.dateStr substringWithRange:NSMakeRange(11,2)] intValue];
[pickerView selectRow:(self.currentYear - kStartYear) inComponent:0 animated:YES];
[pickerView selectRow:(self.currentMonth - 1) inComponent:1 animated:YES];
[pickerView selectRow:(self.currentDay - 1) inComponent:2 animated:YES];
[pickerView selectRow:self.currentHour inComponent:3 animated:YES];
[pickerView selectRow:(self.currentMinute) inComponent:4 animated:YES];
[self pickerView:pickerView didSelectRow:self.currentYear - kStartYear inComponent:0];
[self pickerView:pickerView didSelectRow:(self.currentMonth - 1) inComponent:1];
[self pickerView:pickerView didSelectRow:(self.currentDay - 1) inComponent:2];
[self pickerView:pickerView didSelectRow:(self.currentHour - 1) inComponent:3];
[self pickerView:pickerView didSelectRow:(self.currentMinute -1) inComponent:4];
}
#pragma mark - 私有方法
#pragma mark 判断闰年
- (BOOL)isLeapYear:(NSInteger)year
{
// 如果年数能被4整除,同时不能被100整除
// 能被400整除的是闰年
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
#pragma mark 创建日数组
// 此方法的返回值是void,是为了不改变dayList的指针
- (void)createDayListWithYear:(NSInteger)year month:(NSInteger)month
{
// 日数组是懒加载,需要时在创建
if (self.dayList == nil) {
// 在开发时尽可能要去控制NSMutableArray的容量
self.dayList = [NSMutableArray arrayWithCapacity:31];
} else {
[self.dayList removeAllObjects];
}
// 初始化日数组
/**
年:是否是闰年
月:大月,小月
*/
NSInteger days = 31;
if ([self isLeapYear:year] && month == 2) {
// 闰年的2月
days = 29;
} else if (2 == month) {
// 非闰年的2月
days = 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
// 小月
days = 30;
}
for (NSInteger i = 1; i <= days; i++) {
NSString *str = [NSString stringWithFormat:@"%d日", i];
[self.dayList addObject:str];
}
}
#pragma mark - PickerView的数据源方法
#pragma mark 指定列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 5;
}
#pragma mark 指定行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (0 == component) {
// 年
return self.yearList.count;
} else if (1 == component) {
// 月
return self.monthList.count;
} else if (2 == component) {
// 日
// 需要创建日数组-懒加载
[self createDayListWithYear:self.selectedYear month:self.selectedMonth];
return self.dayList.count;
}
else if (3 == component) {
// 时
return self.hourList.count;
} else // 分
{
return self.minuteList.count;
}
}
#pragma mark - UIPickerViewDelegate
#pragma mark 指定component列row行的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (0 == component) {
return self.yearList[row];
} else if (1 == component) {
return self.monthList[row];
} else if (2 == component){
return self.dayList[row];
}
else if (3 == component) {
return self.hourList[row];
} else {
return self.minuteList[row];
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0) {
return 80;
}
return 50;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// 当用户在月份列选择数值时,需要刷新日期的数组
self.selectedYear = [pickerView selectedRowInComponent:0] + kStartYear;
self.selectedMonth = [pickerView selectedRowInComponent:1] + 1;
NSInteger resetRow = [pickerView selectedRowInComponent:2];
if (component == 1) {
[pickerView reloadComponent:2];
[self pickerView:pickerView didSelectRow:resetRow inComponent:2];
} else if (component == 0 && self.selectedMonth == 2) {
// 如果用户是在年列选择的数值,同时月份中的当前数值是2月,需要刷新数据
[pickerView reloadComponent:2];
[self pickerView:pickerView didSelectRow:resetRow inComponent:2];
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *myView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 330, 30)];
myView.textAlignment = NSTextAlignmentCenter;
myView.font = [UIFont systemFontOfSize:16];
switch (component) {
case 0:
myView.text = self.yearList[row];
break;
case 1:
myView.text = self.monthList[row];
break;
case 2:
myView.text = self.dayList[row];
break;
case 3:
myView.text = self.hourList[row];
break;
case 4:
myView.text = self.minuteList[row];
break;
default:
break;
}
return myView;
}
- (NSString *)getDate
{
NSInteger yearRow = [_pickerView selectedRowInComponent:0];
NSString *year = [self.yearList[yearRow] stringByReplacingOccurrencesOfString:@"年" withString:@""];
NSInteger monthRow = [_pickerView selectedRowInComponent:1];
NSString *month = [self.monthList[monthRow] stringByReplacingOccurrencesOfString:@"月" withString:@""];
NSInteger dayRow = [_pickerView selectedRowInComponent:2];
NSString *day = [self.dayList[dayRow] stringByReplacingOccurrencesOfString:@"日" withString:@""];
NSInteger hourRow = [_pickerView selectedRowInComponent:3];
NSString *hour = [self.hourList[hourRow] stringByReplacingOccurrencesOfString:@"时" withString:@""];
NSInteger minuteRow = [_pickerView selectedRowInComponent:4];
NSString *minute = [self.minuteList[minuteRow] stringByReplacingOccurrencesOfString:@"分" withString:@""];
NSArray *array1 = [NSArray arrayWithObjects:year,month,day, nil];
NSArray *array2 = [NSArray arrayWithObjects:hour,minute, nil];
NSString *date1 = [array1 componentsJoinedByString:@"-"];
NSString *date2 = [array2 componentsJoinedByString:@":"];
NSString *date = [NSString stringWithFormat:@"%@ %@:00",date1,date2];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
return [formatter stringFromDate:[formatter dateFromString:date]];
}
- (void)okClick{
NSString *dateStr = [self getDate];
[self.delegate okTimeView:dateStr];
}
- (void)cancelClick{
[self.delegate cancelTimeView];
}
@end