博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIDatePicker扩展支持年月日时分选择
阅读量:6076 次
发布时间:2019-06-20

本文共 12387 字,大约阅读时间需要 41 分钟。

hot3.png

////  YUDatePicker.h//  YUDatePicker////  Created by yuzhx on 15/4/26.//  Copyright (c) 2015年 BruceYu. All rights reserved.//#import 
#import "NSDate+YU.h"#import "YUDateConfig.h"typedef NS_ENUM(NSInteger, YUUIDatePickerMode) {    UIYUDatePickerModeTime,           // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)        UIYUDatePickerModeDate,           // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)        UIYUDatePickerModeDateAndTime,    // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)    UIYUDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)        UIYUDatePickerModeDateYYYYMMDDHHmm,    // (e.g.  2015年 | 05月 | 01日 | 11时 | 59分)};NS_CLASS_AVAILABLE_IOS(6_0)@interface YUDatePicker : UIControl@property (nonatomic) YUUIDatePickerMode datePickerMode; // default is UIXYDatePickerModeDateAndTime@property (nonatomic, retain) NSLocale   *locale;   // default is [NSLocale currentLocale]. setting nil returns to default@property (nonatomic, copy)   NSCalendar *calendar; // default is [NSCalendar currentCalendar]. setting nil returns to default@property (nonatomic, retain) NSTimeZone *timeZone; // default is nil. use current time zone or time zone from calendar@property (nonatomic, retain) NSDate *date;        // default is current date when picker created. Ignored in countdown timer mode. for that mode, picker starts at 0:00@property (nonatomic, readonly) NSString *dateStr;//return current date Formatter yyyy-MM-dd HH:mm:ss@property (nonatomic, retain) NSDate *minimumDate; // specify min/max date range. default is nil. When min > max, the values are ignored. Ignored in countdown timer mode@property (nonatomic, retain) NSDate *maximumDate; // default is nil@property (nonatomic) NSTimeInterval countDownDuration; // for UIDatePickerModeCountDownTimer, ignored otherwise. default is 0.0. limit is 23:59 (86,399 seconds). value being set is div 60 (drops remaining seconds).@property (nonatomic) NSInteger      minuteInterval;    // display minutes wheel with interval. interval must be evenly divided into 60. default is 1. min is 1, max is 30- (void)setDate:(NSDate *)date animated:(BOOL)animated; // if animated is YES, animate the wheels of time to display the new date@property (nonatomic, assign) BOOL showToolbar;@endtypedef void (^dateBlock)(YUDatePicker *date);@interface YUDatePicker(YU)-(void)showInView:(UIView *)view block:(dateBlock)dateBlock;-(void)hidden;@end
////  YUDatePicker.m//  YUDatePicker////  Created by yuzhx on 15/4/26.//  Copyright (c) 2015年 BruceYu. All rights reserved.//#import "YUDatePicker.h"#define XYNUMBEROFROWS 16384#define XYROSE_NUMBER 99#define PICKERHIGHR  216#define TOOLBARHIGHT 44@interface YUDatePicker()
{    UIPickerView *PickerView;}@property (nonatomic) NSInteger yearIndex;@property (nonatomic) NSInteger monthIndex;@property (nonatomic) NSInteger dayIndex;@property (nonatomic) NSInteger hourIndex;@property (nonatomic) NSInteger minuteIndex;@property (nonatomic) NSInteger secondIndex;@property (nonatomic , strong) NSArray *amPmArray;@property (nonatomic , strong) NSMutableArray *yearArray;@property (nonatomic , strong) NSMutableArray *monthArray;@property (nonatomic , strong) NSMutableArray *dayArray;@property (nonatomic , strong) NSMutableArray *hourArray;@property (nonatomic , strong) NSMutableArray *minuteArray;@property(nonatomic,strong)NSDate *currentDate;@property (strong, nonatomic)UIToolbar *actionToolbar;@end@implementation YUDatePicker- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = [UIColor whiteColor];        if (self.frame.size.height<216 || self.frame.size.width<320)        {            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, PICKERHIGHR);        }    }    return self;}- (instancetype)init{    self = [super init];    if (self) {        self.frame = CGRectMake(0, 0, 0, PICKERHIGHR);    }    return self;}-(NSDate *)date{    NSString *ds = [NSString stringWithFormat:XY_FORMATSTR,                    self.yearArray[self.yearIndex],                    self.monthArray[self.monthIndex],                    self.dayArray[self.dayIndex],                    self.hourArray[self.hourIndex],                    self.minuteArray[self.minuteIndex]                    ];    NSDate *date = [NSDate dateFromString:ds withFormat:XY_FORMAT];    return date;    }-(NSString *)dateStr{    NSString *ds = [NSString stringWithFormat:XY_DATESTR,                    self.yearArray[self.yearIndex],                    self.monthArray[self.monthIndex],                    self.dayArray[self.dayIndex],                    self.hourArray[self.hourIndex],                    self.minuteArray[self.minuteIndex]                    ];    return ds;}-(NSInteger)febNumber{    NSInteger day = [NSDate daysfromYear:[self.yearArray[self.yearIndex] integerValue] andMonth:[self.monthArray[self.monthIndex] integerValue]];    return day;}-(void)setMinimumDate:(NSDate *)minimumDate{    if (!minimumDate) {        _minimumDate = [NSDate dateWithYea:XYPICKER_MINDATE];    }else{        _minimumDate = minimumDate;    }}-(void)setMaximumDate:(NSDate *)maximumDate{    if (!_maximumDate) {        _maximumDate = [NSDate dateWithYea:XYPICKER_MAXDATE];    }else{        _maximumDate = maximumDate;    }}-(NSArray*)setDays:(NSInteger)num{    if ([_dayArray count]) {        [_dayArray removeAllObjects];    }    for (int i = 1; i <= num; i++) {        [_dayArray addObject:[NSString stringWithFormat:@"%02d",i]];    }    return _dayArray;}-(void)setCurrentDate:(NSDate *)currentDate{    if (currentDate) {        _currentDate = currentDate;        NSDateComponents *d = [NSDate dateComponentsFromDate:_currentDate];        self.yearIndex = d.year-XYPICKER_MINDATE;        self.monthIndex = d.month -1;        self.dayIndex = d.day -1;        self.hourIndex = d.hour;        self.minuteIndex = d.minute;        self.secondIndex = d.second;    }}#pragma mark - 初始化赋值操作-(void)initData{    self.yearArray   = [NSMutableArray array];    self.monthArray  = [NSMutableArray array];    self.dayArray    = [NSMutableArray array];    self.hourArray   = [NSMutableArray array];    self.minuteArray = [NSMutableArray array];    self.amPmArray = @[@"AM",@"PM"];        for (int i=0; i
<=XYPICKER_MONTH)            [self.monthArray addObject:num];        if (0
<=XYPICKER_DAY)            [self.dayArray addObject:num];        if (i
=  UIYUDatePickerModeDateYYYYMMDDHHmm ) {        if (!PickerView) {            PickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, TOOLBARHIGHT-22, self.frame.size.width, PICKERHIGHR)];            PickerView.showsSelectionIndicator = YES;            //            PickerView.autoresizingMask = UIViewAutoresizingFlexibleHeight;            PickerView.backgroundColor = [UIColor clearColor];            PickerView.delegate = self;            PickerView.dataSource = self;            [self addSubview:PickerView];        }        [self revertRow:_date animated:NO];            }else{                UIDatePicker *datePicker = [ [ UIDatePicker alloc] initWithFrame:CGRectMake(0,TOOLBARHIGHT-22,self.frame.size.width,PICKERHIGHR-TOOLBARHIGHT)];        datePicker.datePickerMode = (UIDatePickerMode)self.datePickerMode;        datePicker.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;        [datePicker setLocale:self.locale?self.locale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];        datePicker.calendar = self.calendar;        datePicker.timeZone = self.timeZone;        datePicker.date = _date;        datePicker.minimumDate = self.minimumDate;        datePicker.maximumDate = self.maximumDate;        datePicker.minuteInterval = self.minuteInterval;        datePicker.countDownDuration = self.countDownDuration;        [self addSubview:datePicker];    }        if (self.showToolbar) {        _actionToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, TOOLBARHIGHT)];        _actionToolbar.barStyle = UIBarStyleDefault;        [_actionToolbar sizeToFit];                _actionToolbar.layer.borderWidth = 0.35f;        _actionToolbar.layer.borderColor = [[UIColor colorWithWhite:.8 alpha:1.0] CGColor];                        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(actionCancel:)];        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actionDone:)];        [_actionToolbar setItems:[NSArray arrayWithObjects:cancelButton,flexSpace,doneBtn, nil] animated:YES];        [self addSubview:_actionToolbar];    }}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect {    // Drawing code        [self initData];        [self initUI];}#pragma mark - UIPickerViewDataSource- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    if (self.datePickerMode == UIYUDatePickerModeDateYYYYMMDDHHmm){        return 5;    }    return 0;}- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    return XYNUMBEROFROWS;}#pragma mark - UIPickerViewDelegate- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{    switch (self.datePickerMode) {        case UIYUDatePickerModeDateYYYYMMDDHHmm:{            if (component == 0) {                return 75;            }            return 55;        }            break;                    default:            break;    }        return 50;}- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{    return 32;}- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    NSInteger count = 0;    switch (self.datePickerMode) {                    case UIYUDatePickerModeDateYYYYMMDDHHmm:{                        if (component == 0) {                self.yearIndex = row%(count=self.yearArray.count);            }            if (component == 1) {                self.monthIndex = row%(count=self.monthArray.count);                [self setDays:self.febNumber];                [pickerView selectRow:self.dayIndex + self.dayArray.count * XYROSE_NUMBER inComponent:2 animated:NO];            }            if (component == 2) {                self.dayIndex = row%(count=self.dayArray.count);            }            if (component == 3) {                self.hourIndex = row%(count=self.hourArray.count);            }            if (component == 4) {                self.minuteIndex = row%(count=self.minuteArray.count);            }        }            break;                    default:            break;    }        [self selectDid:component];        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        [self revertRow:self.date animated:NO];    });    }- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{    UILabel *l = (UILabel *)view;    if (!l) {        l = [[UILabel alloc] init];        l.textAlignment = NSTextAlignmentCenter;    }    l.backgroundColor = [UIColor clearColor];    [l setFont:[UIFont systemFontOfSize:23]];    UIColor *textColor = [UIColor blackColor];    NSString *title;        switch (self.datePickerMode) {        case UIYUDatePickerModeDateYYYYMMDDHHmm:{            if (component==0) {                title = [NSString stringWithFormat:@"%@年",self.yearArray[row%self.yearArray.count]];                textColor = [self yearColorRow:row];            }            if (component==1) {                title = [NSString stringWithFormat:@"%@月",self.monthArray[row%self.monthArray.count]];                textColor = [self monthColorRow:row];            }            if (component==2) {                                title = [NSString stringWithFormat:@"%@日",self.dayArray[row%self.dayArray.count]];                textColor = [self dayColorRow:row];            }            if (component==3) {                title = [NSString stringWithFormat:@"%@时",self.hourArray[row%self.hourArray.count]];                textColor = [self hourColorRow:row];            }            if (component==4) {                title = [NSString stringWithFormat:@"%@分",self.minuteArray[row%self.minuteArray.count]];                textColor = [self minuteColorRow:row];            }        }            break;                    default:            break;    }    l.text = title;    l.textColor = textColor;    return l;}#pragma mark -#pragma mark - Private- (void)selectDid:(NSInteger)component{    if ([self exceedMin:self.date]) {        [self revertRow:self.minimumDate animated:YES];            }else if ([self exceedMax:self.date]){        [self revertRow:self.maximumDate animated:YES];    }        [self sendActionsForControlEvents:UIControlEventValueChanged];}-(void)revertRow:(NSDate*)date animated:(BOOL)animated{    NSArray *arry = [self getNowDateArry:date];    NSInteger row = 1;    for (int i=0; i

225741_xYSp_868062.png

软件地址:

转载于:https://my.oschina.net/u/868062/blog/406576

你可能感兴趣的文章
quicksort
查看>>
【BZOJ2019】nim
查看>>
四部曲
查看>>
LINUX内核调试过程
查看>>
【HDOJ】3553 Just a String
查看>>
Java 集合深入理解(7):ArrayList
查看>>
2019年春季学期第四周作业
查看>>
linux环境配置
查看>>
ASP.NET MVC中从前台页面视图(View)传递数据到后台控制器(Controller)方式
查看>>
一个想法(续二):换个角度思考如何解决IT企业招聘难的问题!
查看>>
tomcat指定配置文件路径方法
查看>>
linux下查看各硬件型号
查看>>
epoll的lt和et模式的实验
查看>>
Flux OOM实例
查看>>
07-k8s-dns
查看>>
Android 中 ListView 分页加载数据
查看>>
oracle启动报错:ORA-00845: MEMORY_TARGET not supported on this system
查看>>
Go方法
查看>>
Dapper丶DapperExtention,以及AbpDapper之间的关系,
查看>>
搞IT的同学们,你们在哪个等级__那些年发过的帖子
查看>>