JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
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
//
//  Copyright © 2020 dahua. All rights reserved.
//
 
#import "LCDatePick.h"
#import "LCUIKit.h"
#import "LCToolKit.h"
 
@interface LCDatePick ()<UIPickerViewDelegate, UIPickerViewDataSource>
 
@property (nonatomic, strong) NSMutableArray *row;
 
@property (nonatomic) BOOL needCircle;
 
@property (nonatomic) NSInteger minYearInt;
 
@property (nonatomic) NSInteger maxYearInt;
 
@property (nonatomic, strong) UILabel *titleLab;
 
@property (nonatomic, strong) LCButton *cancleBtn;
 
@property (nonatomic, strong) LCButton *confirmBtn;
 
@property (nonatomic, strong) UIPickerView *pickView;
 
@property (nonatomic, strong) UIView *topView;
 
@property (nonatomic, strong) LCDatePickResult *result;
 
@end
 
@implementation LCDatePick
 
#pragma mark - 私有方法
 
- (void)configLayout {
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowOffset = CGSizeMake(3, 0);
    self.layer.shadowRadius = 5;
 
    self.topView = [UIView new];
    [self addSubview:self.topView];
    [self.topView setBorderWithView:self.topView Style:LC_BORDER_DRAW_BOTTOM borderColor:[UIColor dhcolor_c40] borderWidth:1];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
        make.height.mas_equalTo(50);
    }];
 
    self.cancleBtn = [LCButton lcButtonWithType:LCButtonTypeCustom];
    [self.topView addSubview:self.cancleBtn];
    [self.cancleBtn setTitleColor:[UIColor dhcolor_c40] forState:UIControlStateNormal];
    [self.cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(self.topView);
        make.width.mas_equalTo(self.cancleBtn.mas_height);
    }];
 
    self.confirmBtn = [LCButton lcButtonWithType:LCButtonTypeCustom];
    [self.topView addSubview:self.confirmBtn];
    [self.confirmBtn setTitleColor:[UIColor dhcolor_c10] forState:UIControlStateNormal];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.mas_equalTo(self.topView);
        make.width.mas_equalTo(self.confirmBtn.mas_height);
    }];
 
    self.titleLab = [UILabel new];
    [self.topView addSubview:self.titleLab];
    self.titleLab.adjustsFontSizeToFitWidth = YES;
    self.titleLab.minimumScaleFactor = 0;
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.mas_equalTo(self.topView);
        make.left.mas_equalTo(self.cancleBtn.mas_right).offset(5);
        make.right.mas_equalTo(self.confirmBtn.mas_left).offset(-5);
    }];
 
    [self.confirmBtn setTitle:@"Alert_Title_Button_Confirm".lc_T forState:UIControlStateNormal];
    [self.cancleBtn setTitle:@"Alert_Title_Button_Cancle".lc_T forState:UIControlStateNormal];
    self.titleLab.text = @"时间选择器";
 
    self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self addSubview:self.pickView];
    self.pickView.dataSource = self;
    self.pickView.delegate = self;
    [self addSubview:self.pickView];
    [self.pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.topView.mas_bottom).offset(1);
        make.left.bottom.right.mas_equalTo(self);
        make.height.mas_equalTo(239);
    }];
}
 
- (NSMutableArray *)row {
    if (!_row) {
        _row = [NSMutableArray array];
    }
    return _row;
}
 
- (LCDatePickResult *)result {
    if (!_result) {
        _result = [LCDatePickResult new];
//        NSDate *date = [NSDate new];
//        _result.year = date.year;
//        _result.month = date.month;
//        _result.weekOfYear = date.weekOfYear;
//        _result.weekOfMonth = date.weekOfMonth;
//        _result.weekDay = date.weekday;
//        _result.hour = date.hour;
//        _result.minute = date.minute;
//        _result.second = date.second;
    }
    return _result;
}
 
#pragma mark - display
 
/// 初始化pickview
+ (LCDatePick *(^)(void))initialize {
    return ^(void) {
               LCDatePick *datePick = [LCDatePick new];
               datePick.backgroundColor = [UIColor dhcolor_c43];
               datePick.hidden = YES;
               [[datePick topPresentOrRootController].view addSubview:datePick];
               [datePick mas_makeConstraints:^(MASConstraintMaker *make) {
                   make.left.bottom.right.mas_equalTo([datePick topPresentOrRootController].view);
               }];
               [datePick configLayout];
               return datePick;
    };
}
 
/// 弹出pick
- (LCDatePick * (^)(void))start {
    return ^(void) {
               self.hidden = NO;
               NSAssert(([self.row indexOfObject:@"day"] && ![self.row indexOfObject:@"month"]), @"选择日期必须依赖月份");
               return self;
    };
}
 
/// 消失
- (LCDatePick *  (^)(void))dismiss {
    return ^(void) {
               self.hidden = YES;
               self.row = nil;
               CABasicAnimation *baseAni = [CABasicAnimation animation];
               baseAni.keyPath = @"position.y";
               baseAni.toValue = @(800);
               [baseAni setDuration:2];
               [self.layer addAnimation:baseAni forKey:nil];
               dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self removeFromSuperview];
        });
               return self;
    };
}
 
- (UIViewController *)topPresentOrRootController {
    UIViewController *rootVC = [[UIApplication sharedApplication] delegate].window.rootViewController;
    UIViewController *presentVc = rootVC.presentedViewController;
    UIViewController *targetVc;
    while (presentVc && ![presentVc isKindOfClass:[UIAlertController class]]) {
        targetVc = presentVc;
        presentVc = presentVc.presentedViewController;
    }
 
    if (targetVc) {
        return targetVc;
    }
 
    return rootVC;
}
 
#pragma mark - config
 
/// 向pick中添加年
- (LCDatePick *(^)(void))addYear {
    return ^(void) {
               [self.row addObject:@"year"];
               return self;
    };
}
 
/// 向pick中添加月
- (LCDatePick *(^)(void))addMonth {
    return ^(void) {
               [self.row addObject:@"month"];
               return self;
    };
}
 
/// 向pick中添加周
- (LCDatePick *(^)(void))addWeekOfYear {
    return ^(void) {
               [self.row addObject:@"weekofyear"];
               return self;
    };
}
 
- (LCDatePick *(^)(void))addWeekOfMonth {
    return ^(void) {
               [self.row addObject:@"weekofmonth"];
               return self;
    };
}
 
/// 向pick中添加周几
- (LCDatePick *(^)(void))addWeekDay {
    return ^(void) {
               [self.row addObject:@"weekDay"];
               return self;
    };
}
 
/// 向pick中添加天
- (LCDatePick *(^)(void))addDay {
    return ^(void) {
               [self.row addObject:@"day"];
               return self;
    };
}
 
/// 向pick中添加小时
- (LCDatePick *(^)(void))addHour {
    return ^(void) {
               [self.row addObject:@"hour"];
               return self;
    };
}
 
/// 向pick中添加分钟
- (LCDatePick *(^)(void))addMinute {
    return ^(void) {
               [self.row addObject:@"minute"];
               return self;
    };
}
 
/// 向pick中添加n秒
- (LCDatePick *(^)(void))addSecond {
    return ^(void) {
               [self.row addObject:@"second"];
               return self;
    };
}
 
/// 是否需要无限循环
- (LCDatePick *(^)(BOOL circle))circle {
    return ^(BOOL circle) {
               self.needCircle = circle;
               return self;
    };
}
 
/// 最小年份偏移量
- (LCDatePick *(^)(NSInteger min))minYear {
    return ^(NSInteger min) {
               self.minYearInt = min;
               return self;
    };
}
 
/// 最大年份偏移量
- (LCDatePick *(^)(NSInteger max))maxYear {
    return ^(NSInteger max) {
               self.maxYearInt = max;
               return self;
    };
}
 
/// 取消按钮文字显示
- (LCDatePick *(^)(NSString *cancleTitle))cancleTitle {
    return ^(NSString *cancleTitle) {
               [self.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal];
               return self;
    };
}
 
/// 确认按钮文字显示
- (LCDatePick *(^)(NSString *confirmTitle))confirmTitle {
    return ^(NSString *confirmTitle) {
               [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
               return self;
    };
}
 
/// 主题显示
- (LCDatePick *(^)(NSString *title))title {
    return ^(NSString *title) {
               self.titleLab.text = title;
               return self;
    };
}
 
#pragma mark - pickview
 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.row.count;
}
 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [self getRowNumberWithComponent:component];
}
 
//- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    return [self displayForRow:row AtComponent:component];
//}
 
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self selectRow:row AtComponent:component];
}
 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *lab = (UILabel *)view;
    if (!lab) {
        lab = [UILabel new];
    }
    lab.textAlignment = NSTextAlignmentCenter;
    lab.attributedText = [self displayForRow:row AtComponent:component];
    return lab;
}
 
- (NSMutableAttributedString *)displayForRow:(NSInteger)row AtComponent:(NSInteger)component {
    if (self.row.count < component) {
        return @"";
    }
    NSString *componentType = [self.row objectAtIndex:component];
    NSString *str = nil;
    if ([componentType isEqualToString:@"year"]) {
        str = [NSString stringWithFormat:@"%ld年", self.minYearInt + row];
    } else if ([componentType isEqualToString:@"month"]) {
        str = [NSString stringWithFormat:@"%ld月", row + 1];
    } else if ([componentType isEqualToString:@"weekofyear"]) {
        str = [NSString stringWithFormat:@"第%ld周", row + 1];
    } else if ([componentType isEqualToString:@"weekofmonth"]) {
        str = [NSString stringWithFormat:@"第%ld个", row + 1];
        if ((row + 1) == 5) {
            str = @"最后一个";
        }
    } else if ([componentType isEqualToString:@"weekDay"]) {
        str = [NSString stringWithFormat:@"%@", [self numberChangeToString:row]];
    } else if ([componentType isEqualToString:@"day"]) {
        str = [NSString stringWithFormat:@"%ld日", row + 1];
    } else if ([componentType isEqualToString:@"hour"]) {
        str = [NSString stringWithFormat:@"%ld点", row];
    } else if ([componentType isEqualToString:@"minute"]) {
        str = [NSString stringWithFormat:@"%ld分", row];
    } else if ([componentType isEqualToString:@"second"]) {
        str = [NSString stringWithFormat:@"%ld秒", row];
    } else {
    }
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attrStr addAttribute:NSForegroundColorAttributeName value:UIColor.blackColor range:NSMakeRange(0, attrStr.length)];
    return attrStr;
}
 
- (NSString *)numberChangeToString:(NSInteger)weekDay {
    switch (weekDay) {
        case 0: {
            return @"周日";
        }
        break;
        case 1: {
            return @"周一";
        }
        break;
        case 2: {
            return @"周二";
        }
        break;
        case 3: {
            return @"周三";
        }
        break;
        case 4: {
            return @"周四";
        }
        break;
        case 5: {
            return @"周五";
        }
        break;
        case 6: {
            return @"周六";
        }
        break;
 
        default: {
            return @"周一";
        }
        break;
    }
}
 
- (void)selectRow:(NSInteger)row AtComponent:(NSInteger)component {
    if (self.row.count < component) {
        return;
    }
    NSString *componentType = [self.row objectAtIndex:component];
 
    if ([componentType isEqualToString:@"year"]) {
        self.result.year = self.minYearInt + row;
        if ([self.row containsObject:@"day"]) {
            [self.pickView reloadComponent:[self.row indexOfObject:@"day"]];
        }
    } else if ([componentType isEqualToString:@"month"]) {
        self.result.month = row + 1;
        if ([self.row containsObject:@"day"]) {
            [self.pickView reloadComponent:[self.row indexOfObject:@"day"]];
        }
    } else if ([componentType isEqualToString:@"weekofyear"]) {
        self.result.weekOfYear = row + 1;
    } else if ([componentType isEqualToString:@"weekofmonth"]) {
        self.result.weekOfMonth = row + 1;
    } else if ([componentType isEqualToString:@"weekDay"]) {
        self.result.weekDay = row;
    } else if ([componentType isEqualToString:@"day"]) {
        self.result.day = row + 1;
    } else if ([componentType isEqualToString:@"hour"]) {
        self.result.hour = row;
    } else if ([componentType isEqualToString:@"minute"]) {
        self.result.minute = row;
    } else if ([componentType isEqualToString:@"second"]) {
        self.result.second = row;
    } else {
    }
}
 
- (NSInteger)getRowNumberWithComponent:(NSInteger)component {
    NSString *componentType = [self.row objectAtIndex:component];
    if ([componentType isEqualToString:@"year"]) {
        return (self.maxYearInt - self.minYearInt + 1);
    } else if ([componentType isEqualToString:@"month"]) {
        return 12;
    } else if ([componentType isEqualToString:@"weekofyear"]) {
        return 52;
    } else if ([componentType isEqualToString:@"weekofmonth"]) {
        return 5;
    } else if ([componentType isEqualToString:@"weekDay"]) {
        return 7;
    } else if ([componentType isEqualToString:@"day"]) {
        return [self numberOfDayInMonthWithYear:self.result.year Month:self.result.month];
    } else if ([componentType isEqualToString:@"hour"]) {
        return 24;
    } else if ([componentType isEqualToString:@"minute"]) {
        return 60;
    } else if ([componentType isEqualToString:@"second"]) {
        return 60;
    } else {
        return 0;
    }
}
 
/**
 根据给定的日期返回该月的天数
 */
- (NSInteger)numberOfDayInMonthWithYear:(NSInteger)year Month:(NSInteger)month {
    NSDate *date = [self dateWithdateSr:[NSString stringWithFormat:@"%ld-%02ld", year, month]];
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 通过该方法计算特定日期月份的天数
    NSRange monthRange =  [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
 
    return monthRange.length;
}
 
/**
 根据给出的日期获得NSDate
 
 @param dateStr 日期
 @return 对应的NSDate
 */
- (NSDate *)dateWithdateSr:(NSString *)dateStr {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    // 此处根据需求改对应的日期格式
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
 
    return date;
}
 
#pragma mark - action
/// 取消按钮点击事件
- (LCDatePick *)cancleHandle:(void (^)(void))resultBlock {
    weakSelf(self);
    self.cancleBtn.touchUpInsideblock = ^(LCButton *_Nonnull btn) {
        resultBlock();
        weakself.dismiss();
    };
    return self;
}
 
///// 确定按钮点击事件
- (LCDatePick *)confirmHandle:(void (^)(LCDatePickResult *result))resultBlock {
    weakSelf(self);
    self.confirmBtn.touchUpInsideblock = ^(LCButton *_Nonnull btn) {
//        if (![weakself.row containsObject:@"year"]) {
//            weakself.result.year = -1;
//        }
//        if (![weakself.row containsObject:@"month"]) {
//            weakself.result.month = -1;
//        }
//        if (![weakself.row containsObject:@"weekofyear"]) {
//            weakself.result.weekOfYear = -1;
//        }
//        if (![weakself.row containsObject:@"weekofmonth"]) {
//            weakself.result.weekOfMonth = -1;
//        }
//        if (![weakself.row containsObject:@"weekDay"]) {
//            weakself.result.weekDay = -1;
//        }
//        if (![weakself.row containsObject:@"day"]) {
//            weakself.result.day = -1;
//        }
//        if (![weakself.row containsObject:@"hour"]) {
//            weakself.result.hour = -1;
//        }
//        if (![weakself.row containsObject:@"minute"]) {
//            weakself.result.minute = -1;
//        }
//        if (![weakself.row containsObject:@"second"]) {
//            weakself.result.second = -1;
//        }
        weakself.dismiss();
        resultBlock(weakself.result);
    };
    return self;
}
 
///// 页面消失时事件
//-(LCDatePick*)dismissHandle:(void(^)(void))resultBlock;
 
@end
 
@implementation LCDatePickResult
 
- (NSInteger)year {
    if (_year == 0) {
        _year = 1;
    }
    return _year;
}
 
//月份 1-12
- (NSInteger)month {
    if (_month == 0) {
        _month = 1;
    }
    return _month;
}
 
//周 1-5
- (NSInteger)weekOfMonth {
    if (_weekOfMonth == 0) {
        _weekOfMonth = 1;
    }
    return _weekOfMonth;
}
 
//周 1-52
- (NSInteger)weekOfYear {
    if (_weekOfYear == 0) {
        _weekOfYear = 1;
    }
    return _weekOfYear;
}
 
//周几 1-7
- (NSInteger)weekDay {
    return _weekDay;
}
 
//日期 1-31
- (NSInteger)day {
    if (_day == 0) {
        _day = 1;
    }
    return _day;
}
 
@end