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


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liu943367080/article/details/80689226
个人分类: ios-objectc
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭