OC记录2--UIDatePicker

官方描述:

A control used for the inputting of date and time values.
You can use a date picker to allow a user to enter either a point in time (calendar date, time value or both) or a time interval (for example for a timer). The date picker reports interactions to its associated target object.

译文:

用于输入日期和时间值的控件。您可以使用日期选择器来允许用户输入时间点(日历日期,时间值或两者)或时间间隔(例如,计时器)。日期选择器向与其关联的目标对象报告交互。

UIDatePicker头文件.h查看

UIDatePicker继承关系:UIDatePicker->UIControl->UIView

1
NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIDatePicker : UIControl <NSCoding>

属性

UIDatePicker样式:枚举UIDatePickerMode,该属性默认是UIDatePickerModeDateAndTime
1
@property (nonatomic) UIDatePickerMode datePickerMode; // default is UIDatePickerModeDateAndTime

枚举UIDatePickerMode:

1
2
3
4
5
6
typedef NS_ENUM(NSInteger, UIDatePickerMode) {
UIDatePickerModeTime, // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
UIDatePickerModeDate, // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
UIDatePickerModeDateAndTime, // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
} __TVOS_PROHIBITED

UIDatePickerModeTime:只展示小时,分钟和AM(早上),PM(下午)

image

UIDatePickerModeDate:展示年月日

image

UIDatePickerModeDateAndTime:展示日期和时间

image

UIDatePickerModeCountDownTimer:倒计时模式

image

locale:本地化

该属性表示的是设置日期展示的语言,不进行设置时,默认手机系统当前语言;

1
@property (nullable, nonatomic, strong) NSLocale   *locale;   // default is [NSLocale currentLocale]. setting nil returns to default

创建方式:

1
2
3
4
5
6
7
[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hant_TW"];

//简体中文:zh_Hans_CN,zh_Hans_HK,zh_Hans_MO,zh_Hans,zh,zh_Hans_SG,zh_Hans_CN
//繁体中文:zh_Hant,zh_Hant_HK,zh_Hant_MO,zh_Hant_TW

//获取可以的本地化标识
[NSLocale availableLocaleIdentifiers];
calendar:不进行设置时,默认是当前日期
1
@property (null_resettable, nonatomic, copy)   NSCalendar *calendar; // default is [NSCalendar currentCalendar]. setting nil returns to default
timeZone:时区,不进行设置时,默认为nil,日期来自属性calendar
1
@property (nullable, nonatomic, strong) NSTimeZone *timeZone; // default is nil. use current time zone or time zone from calendar
date:默认选中日期,当不进行设置时,默认当前日期;

并且UIDatePickerModeCountDownTimer模式下的设置对其并没有作用;

1
@property (nonatomic, strong) NSDate *date;        // default is current date when picker created. Ignored in countdown timer mode. for that mode, picker starts at 0:00
minimumDate:最小能选择的日期,当不设置时,理论上没有最小限制;

并且如果min > max 该值会失效;在UIDatePickerModeCountDownTimer无效

1
@property (nullable, nonatomic, strong) NSDate *minimumDate; // specify min/max date range. default is nil. When min > max, the values are ignored. Ignored in countdown timer mode
maximumDate:最大能选择的日期,当不设置时,理论上没有最大限制;
1
@property (nullable, nonatomic, strong) NSDate *maximumDate; // default is nil
countDownDuration:倒计时剩下秒数,最大23:59(86399秒)

只在UIDatePickerModeCountDownTimer有效

1
@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).
minuteInterval:分钟间隔,默认是1,最大是30
1
@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

方法

滑动到设置的时间日期,是否需要动画效果;

1
- (void)setDate:(NSDate *)date animated:(BOOL)animated; // if animated is YES, animate the wheels of time to display the new date