DatePickerView.m
#import "DatePickerView.h"
@interface DatePickerView () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (strong, nonatomic) UIPickerView *pickerView; // 選擇器
@property (strong, nonatomic) UIView *toolView; // 工具條
@property (strong, nonatomic) UILabel *titleLbl; // 標題
@property (strong, nonatomic) UILabel *cancelLbl; // 取消
@property (strong, nonatomic) UILabel *confirmLbl; // 確認
@property (strong, nonatomic) NSMutableArray *dataArray; // 資料來源
@property (copy, nonatomic) NSString *selectStr; // 選中的時間
@property (strong, nonatomic) NSMutableArray *yearArr; // 年陣列
@property (strong, nonatomic) NSMutableArray *monthArr; // 月陣列
@property (strong, nonatomic) NSMutableArray *dayArr; // 日陣列
@property (strong, nonatomic) NSArray *timeArr; // 當前時間陣列
@property (copy, nonatomic) NSString *year; // 選中年
@property (copy, nonatomic) NSString *month; //選中月
@property (copy, nonatomic) NSString *day; //選中日
@end
#define THColorRGB(rgb) [UIColor colorWithRed:(rgb)/255.0 green:(rgb)/255.0 blue:(rgb)/255.0 alpha:1.0]
@implementation DatePickerView
#pragma mark - init
/// 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.timeArr = [NSArray array];
self.dataArray = [NSMutableArray array];
[self.dataArray addObject:self.yearArr];
[self.dataArray addObject:self.monthArr];
[self.dataArray addObject:self.dayArr];
[self configData];
[self configToolView];
[self configPickerView];
}
return self;
}
- (void)configData {
self.isSlide = YES;
self.minuteInterval = 5;
NSDate *date = [NSDate date];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
self.date = [dateFormatter stringFromDate:date];
}
#pragma mark - 配置介面
/// 配置工具條
- (void)configToolView {
self.toolView = [[UIView alloc] init];
self.toolView.frame = CGRectMake(0, 0, self.frame.size.width, 44);
[self addSubview:self.toolView];
CGFloat width = self.toolView.frame.size.width;
CGFloat height = self.toolView.frame.size.height;
self.titleLbl = [[UILabel alloc] init];
self.titleLbl.frame = CGRectMake(60, 2, self.frame.size.width - 120, 40);
self.titleLbl.textAlignment = NSTextAlignmentCenter;
self.titleLbl.textColor = THColorRGB(34);
[self.toolView addSubview:self.titleLbl];
self.cancelLbl = [[UILabel alloc] init];
self.cancelLbl.frame = CGRectMake(0, 2, 100, 40);
self.cancelLbl.textAlignment = NSTextAlignmentCenter;
self.cancelLbl.textColor = THColorRGB(34);
self.cancelLbl.userInteractionEnabled=YES;
UITapGestureRecognizer *cancelLblTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelBtnClick)];
[self.cancelLbl addGestureRecognizer:cancelLblTapGestureRecognizer];
[self.toolView addSubview:self.cancelLbl];
self.confirmLbl = [[UILabel alloc] init];
self.confirmLbl.frame = CGRectMake(self.toolView.frame.size.width - 100, 2, 100, 40);
self.confirmLbl.textAlignment = NSTextAlignmentCenter;
self.confirmLbl.textColor = THColorRGB(34);
self.confirmLbl.userInteractionEnabled=YES;
UITapGestureRecognizer *confirmLblTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(saveBtnClick)];
[self.confirmLbl addGestureRecognizer:confirmLblTapGestureRecognizer];
[self.toolView addSubview:self.confirmLbl];
}
/// 配置UIPickerView
- (void)configPickerView {
self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.toolView.frame), self.frame.size.width, self.frame.size.height - 44)];
self.pickerView.backgroundColor = [UIColor whiteColor];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.pickerView.showsSelectionIndicator = YES;
[self addSubview:self.pickerView];
}
- (void)setTitle:(NSString *)title {
_title = title;
self.titleLbl.text = title;
}
- (void)setCancel:(NSString *)body {
self.cancelLbl.text = body;
}
- (void)setConfirm:(NSString *)body {
self.confirmLbl.text = body;
}
- (void)setDate:(NSString *)date {
_date = date;
NSString *newDate = [[date stringByReplacingOccurrencesOfString:@"-" withString:@" "] stringByReplacingOccurrencesOfString:@":" withString:@" "];
NSMutableArray *timerArray = [NSMutableArray arrayWithArray:[newDate componentsSeparatedByString:@" "]];
[timerArray replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%@年", timerArray[0]]];
[timerArray replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%@月", timerArray[1]]];
[timerArray replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"%@日", timerArray[2]]];
self.timeArr = timerArray;
}
- (void)show {
self.year = self.timeArr[0];
self.month = [NSString stringWithFormat:@"%ld月", [self.timeArr[1] integerValue]];
self.day = [NSString stringWithFormat:@"%ld日", [self.timeArr[2] integerValue]];
[self.pickerView selectRow:[self.yearArr indexOfObject:self.year] inComponent:0 animated:YES];
/// 重新格式化轉一下,是因為如果是09月/日/時,資料來源是9月/日/時,就會出現崩潰
[self.pickerView selectRow:[self.monthArr indexOfObject:self.month] inComponent:1 animated:YES];
[self.pickerView selectRow:[self.dayArr indexOfObject:self.day] inComponent:2 animated:YES];
NSLog(@"==========show=====%@ %@ %@",self.year,self.month,self.day);
/// 重新整理日
[self refreshDay];
}
#pragma mark - 點選方法
/// 儲存按鈕點選方法
- (void)saveBtnClick {
NSLog(@"點選了儲存");
NSString *month = self.month.length == 3 ? [NSString stringWithFormat:@"%ld", [self.month integerValue]] : [NSString stringWithFormat:@"0%ld", [self.month integerValue]];
NSString *day = self.day.length == 3 ? [NSString stringWithFormat:@"%ld", [self.day integerValue]] : [NSString stringWithFormat:@"0%ld", [self.day integerValue]];
self.selectStr = [NSString stringWithFormat:@"%ld-%@-%@", [self.year integerValue], month, day];
if ([self.delegate respondsToSelector:@selector(datePickerViewSaveBtnClickDelegate:)]) {
[self.delegate datePickerViewSaveBtnClickDelegate:self.selectStr];
}
}
/// 取消按鈕點選方法
- (void)cancelBtnClick {
NSLog(@"點選了取消");
if ([self.delegate respondsToSelector:@selector(datePickerViewCancelBtnClickDelegate)]) {
[self.delegate datePickerViewCancelBtnClickDelegate];
}
}
#pragma mark - UIPickerViewDelegate and UIPickerViewDataSource
/// UIPickerView返回多少組
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return self.dataArray.count;
}
/// UIPickerView返回每組多少條資料
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.dataArray[component] count] * 200;
}
/// UIPickerView選擇哪一行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSInteger time_integerValue = [self.timeArr[component] integerValue];
NSLog(@"component is %d",component);
switch (component)
{
case 0: { // 年
NSString *year_integerValue = self.yearArr[row%[self.dataArray[component] count]];
if (!self.isSlide) {
self.year = year_integerValue;
return;
}
{
self.year = year_integerValue;
/// 重新整理日
[self refreshDay];
}
} break;
case 1: { // 月
NSString *month_value = self.monthArr[row%[self.dataArray[component] count]];
if (!self.isSlide) {
self.month = month_value;
return;
}
{
self.month = month_value;
/// 重新整理日
[self refreshDay];
}
} break;
case 2: { // 日
//NSString *day_value = self.dayArr[row%[self.dataArray[component] count]];
if (!self.isSlide) {
self.day = [ [NSString stringWithFormat:@"%d",(row+1)%[self.dataArray[component] count]] stringByAppendingString:@"日"];
return;
}
{
self.day = [ [NSString stringWithFormat:@"%d",(row+1)%[self.dataArray[component] count]] stringByAppendingString:@"日"];
/// 重新整理日
[self refreshDay];
}
break;
}
}
}
/// UIPickerView返回每一行資料
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.dataArray[component] objectAtIndex:row%[self.dataArray[component] count]];
}
/// UIPickerView返回每一行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 44;
}
/// UIPickerView返回每一行的View
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *titleLbl;
if (!view) {
titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, 44)];
titleLbl.font = [UIFont systemFontOfSize:15];
titleLbl.textAlignment = NSTextAlignmentCenter;
} else {
titleLbl = (UILabel *)view;
}
titleLbl.text = [self.dataArray[component] objectAtIndex:row%[self.dataArray[component] count]];
return titleLbl;
}
- (void)pickerViewLoaded:(NSInteger)component row:(NSInteger)row{
NSUInteger max = 16384;
NSUInteger base10 = (max/2)-(max/2)%row;
[self.pickerView selectRow:[self.pickerView selectedRowInComponent:component] % row + base10 inComponent:component animated:NO];
}
/// 獲取年份
- (NSMutableArray *)yearArr {
if (!_yearArr) {
_yearArr = [NSMutableArray array];
for (int i = 1970; i < 2099; i ++) {
[_yearArr addObject:[NSString stringWithFormat:@"%d年", i]];
}
}
return _yearArr;
}
/// 獲取月份
- (NSMutableArray *)monthArr {
// NSDate *today = [NSDate date];
// NSCalendar *c = [NSCalendar currentCalendar];
// NSRange days = [c rangeOfUnit:NSCalendarUnitMonth inUnit:NSCalendarUnitYear forDate:today];
if (!_monthArr) {
_monthArr = [NSMutableArray array];
for (int i = 1; i <= 12; i ++) {
[_monthArr addObject:[NSString stringWithFormat:@"%d月", i]];
}
}
return _monthArr;
}
/// 獲取當前月的天數
- (NSMutableArray *)dayArr {
if (!_dayArr) {
_dayArr = [NSMutableArray array];
for (int i = 1; i <= 31; i ++) {
[_dayArr addObject:[NSString stringWithFormat:@"%d日", i]];
}
}
return _dayArr;
}
// 比較選擇的時間是否小於當前時間
- (int)compareDate:(NSString *)date01 withDate:(NSString *)date02{
int ci;
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy年,MM月,dd日,HH時,mm分"];
NSDate *dt1 = [[NSDate alloc] init];
NSDate *dt2 = [[NSDate alloc] init];
dt1 = [df dateFromString:date01];
dt2 = [df dateFromString:date02];
NSComparisonResult result = [dt1 compare:dt2];
switch (result) {
//date02比date01大
case NSOrderedAscending: ci=1;break;
//date02比date01小
case NSOrderedDescending: ci=-1;break;
//date02=date01
case NSOrderedSame: ci=0;break;
default: NSLog(@"erorr dates %@, %@", dt2, dt1);break;
}
NSLog(@"ci is %d",ci);
return ci;
}
- (void)refreshDay {
NSMutableArray *arr = [NSMutableArray array];
for (int i = 1; i < [self getDayNumber:self.year.integerValue month:[self.month integerValue]].integerValue + 1; i ++) {
[arr addObject:[NSString stringWithFormat:@"%d日", i]];
}
[self.dataArray replaceObjectAtIndex:2 withObject:arr];
[self.pickerView reloadComponent:2];
}
- (NSString *)getDayNumber:(NSInteger)year month:(NSInteger)month{
NSArray *days = @[@"31", @"28", @"31", @"30", @"31", @"30", @"31", @"31", @"30", @"31", @"30", @"31"];
if (2 == month && 0 == (year % 4) && (0 != (year % 100) || 0 == (year % 400))) {
return @"29";
}
return days[month - 1];
}
@end