iOS带年月日时分秒的时间选择器

在网上下载的一个时间选择器,挺简洁好用的,所以进行了改造,加了分类小标题和秒的选择还有代理,希望能帮的上对时间精确度有特殊要求的同胞,直接拷代码上了

.h文件

#import

@protocol DatePickViewDelegate

-(void)timeIsAhoice:(NSArray *)arrs;

- (void)didClickFinishBtn:(NSArray *)array;

@end

@interface DatePickerView : UIView

@property(nonatomic,weak)iddelagate;

@property(nonatomic,strong)UIPickerView *DatePcikView;

@property(nonatomic,assign)int startTimeYear;

@end

.m文件

#import "DatePickerView.h"@interface DatePickerView() {

    NSArray *_tempArray;

}

@property(nonatomic,strong)NSMutableArray *dateStrs;

@property(nonatomic,strong)NSArray *currentTimeArrs;

@property(nonatomic,assign)int tempYear;

@property(nonatomic,assign)int tempMouth;

@property(nonatomic,strong)UIButton *cancelbut;

@property (nonatomic , strong) UIView *viewCell;

@end

@implementation DatePickerView

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSLog(@"xxxxxx");

}

- (UIView *)viewCell {

    if (!_viewCell) {

        _viewCell = [UIView new];

        NSArray *temp = @[@"年",@"月",@"日",@"时",@"分",@"秒"];

        CGFloat lblWidth = (SCREEN_WIDTH-32)/temp.count;

        for (int i = 0; i

            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i*lblWidth, 0, lblWidth, 15)];

            label.text = temp[i];

            label.font = [UIFont systemFontOfSize:12];

            label.textColor = [UIColor colorFromHex:@"999999"];

            label.textAlignment = NSTextAlignmentCenter;

            [_viewCell addSubview:label];

    }

}

return _viewCell;

}

-(UIButton *)cancelbut{

    if (!_cancelbut) {

        _cancelbut = [UIButton new];

        [_cancelbut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [_cancelbut setTitle:@"确定" forState:UIControlStateNormal];

        [_cancelbut addTarget:self action:@selector(cancelbutClick) forControlEvents:UIControlEventTouchUpInside];

    }

    return _cancelbut;

}

-(void)cancelbutClick{

    if ([self.delagate respondsToSelector:@selector(didClickFinishBtn:)]) {

        [self.delagate didClickFinishBtn:_tempArray];

    }

}

-(UIPickerView *)DatePcikView{

    if (!_DatePcikView) {

        _DatePcikView = [UIPickerView new];

        _DatePcikView.delegate = self;

        _DatePcikView.dataSource = self;


    }

    return _DatePcikView;

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return self.dateStrs.count;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return [self.dateStrs[component] count];

}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *myView = nil;

    myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];

    myView.textAlignment = NSTextAlignmentCenter;

    myView.text = self.dateStrs[component][row];

    myView.font = [UIFont systemFontOfSize:14];

    myView.backgroundColor = [UIColor clearColor];

    return myView;

}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if (component == 0) {

        NSArray *arrs = [self monthOfDaysByMonthNumber:self.tempMouth andByYear:[self.dateStrs[0][row] intValue]];

        self.tempYear = [self.dateStrs[0][row] intValue];

        [self.dateStrs replaceObjectAtIndex:2 withObject:arrs];

        [pickerView reloadAllComponents];

    }else if (component == 1) {

        NSArray *arrs = [self monthOfDaysByMonthNumber:[self.dateStrs[1][row] intValue] andByYear:self.tempYear];

        self.tempMouth = [self.dateStrs[1][row] intValue];

        [self.dateStrs replaceObjectAtIndex:2 withObject:arrs];

        [pickerView reloadAllComponents];

    }


    NSMutableArray *tempArr = [NSMutableArray array];

    for (int i = 0; i < self.dateStrs.count; i++) {

        [tempArr addObject:self.dateStrs[i][[pickerView selectedRowInComponent:i]]];

    }

    _tempArray = tempArr;


    if ([self.delagate respondsToSelector:@selector(timeIsAhoice:)]) {

        [self.delagate timeIsAhoice:tempArr];

    }

}

-(void)layoutSubviews{

    [super layoutSubviews];

    _cancelbut.frame = CGRectMake(self.frame.size.width-60, 0, 50, 40);

    _DatePcikView.frame = CGRectMake(16, 52, self.frame.size.width-32, self.frame.size.height-52);

    _viewCell.frame = CGRectMake(16, 40, SCREEN_WIDTH-32, 12);

    [_DatePcikView selectRow:[self.currentTimeArrs[0] intValue] - 1990 inComponent:0 animated:NO];

    self.tempYear = [self.currentTimeArrs[0] intValue];

    [_DatePcikView selectRow:[self.currentTimeArrs[1]intValue] - 1 inComponent:1 animated:NO];

    self.tempMouth = [self.currentTimeArrs [1] intValue];

    [_DatePcikView selectRow:[self.currentTimeArrs[2]intValue] - 1 inComponent:2 animated:NO];

    [_DatePcikView selectRow:[self.currentTimeArrs[3]intValue] inComponent:3 animated:NO];

    [_DatePcikView selectRow:[self.currentTimeArrs[4]intValue] inComponent:4 animated:NO];

    [_DatePcikView selectRow:[self.currentTimeArrs[5]intValue] inComponent:5 animated:NO];

    _tempArray = @[self.currentTimeArrs[0],self.currentTimeArrs[1],self.currentTimeArrs[2],self.currentTimeArrs[3],self.currentTimeArrs[4],self.currentTimeArrs[5]];

}

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        [self Getdates];

        [self addSubview:self.DatePcikView];

        [self addSubview:self.cancelbut];

        [self addSubview:self.viewCell];

    }

    return self;

}

-(void)Getdates{

    self.dateStrs = [NSMutableArray array];

    self.currentTimeArrs =[self getCurrentTime];

    for (int i = 0; i < self.currentTimeArrs.count; i++) {

        NSMutableArray *tempArrs = [NSMutableArray array];

        switch (i) {

            case 0:

            {

                for (int j = 1990; j < 2030; j++) {

                    [tempArrs addObject:[NSString stringWithFormat:@"%d",j]];

                }

                [self.dateStrs addObject:tempArrs ];

            }

                break;

            case 1:

            {

                for (int j = 1; j < 13; j++) {

                    [tempArrs addObject:[NSString stringWithFormat:@"%02d",j]];

                }

                [self.dateStrs addObject:tempArrs];

            }

                break;

            case 2:

            {

                tempArrs = [NSMutableArray arrayWithArray:[self monthOfDaysByMonthNumber:[self.currentTimeArrs[1] intValue] andByYear:[self.currentTimeArrs[0] intValue]]];

                [_dateStrs addObject:tempArrs];

            }

                break;

            case 3:

            {

                for (int j = 0; j < 24; j++) {

                    [tempArrs addObject:[NSString stringWithFormat:@"%02d",j]];

                }

                [self.dateStrs addObject:tempArrs];

            }

                break;

            case 4:

            {

                for (int j = 0; j < 60; j++) {

                    [tempArrs addObject:[NSString stringWithFormat:@"%02d",j]];

                }

                [self.dateStrs addObject:tempArrs];

            }

                break;

            case 5:

            {

                for (int j = 0; j < 60; j++) {

                    [tempArrs addObject:[NSString stringWithFormat:@"%02d",j]];

                }

                [self.dateStrs addObject:tempArrs];

            }

                break;

        }

    }

}

-(NSArray *)getCurrentTime{

    NSDate *  senddate=[NSDate date];

    NSDateFormatter  *dateformatterDay=[[NSDateFormatter alloc] init];

    [dateformatterDay setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];

    NSString *  locationStringDay=[dateformatterDay stringFromDate:senddate];

    return [locationStringDay componentsSeparatedByString:@"-"];

}

-(NSArray *)monthOfDaysByMonthNumber:(int)monthNumber andByYear:(int)year{

    NSMutableArray *daysArr = [NSMutableArray array];

    int j = 0;

    switch (monthNumber) {

        case 1:case 3:case 5:case 7:case 8:case 10:case 12:

        {

            j = 31;

        }

            break;

        case 2:

        {

            if((year%4 == 0 && year%100 != 0)|| year%400 == 0){

                j = 29;

            }else{

                j = 28;

            }

        }

            break;

        default:

        {

            j = 30;

        }

            break;

    }

    for (int index = 1; index <= j; index ++) {

        [daysArr addObject:[NSString stringWithFormat:@"%d",index]];

    }

    return daysArr;

}

@end

在controller中的调用,添加代理

// 实现代理方法

- (void)didClickFinishBtn:(NSArray *)array {

// 点击确定按钮返回时间,数组,六个元素,年月日时分秒

}

// 初始化

- (DatePickerView *)dateView {

    if (!_dateView) {

        _dateView = [[DatePickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];

        _dateView.delagate = self;

        _dateView.backgroundColor = [UIColor whiteColor];

    }

    return _dateView;

}

使用

_textField.inputView = self.dateView;

你可能感兴趣的:(iOS带年月日时分秒的时间选择器)