标签:
1、基本用法:
UIDatePicker * datePicker = [[UIDatePicker alloc] init]; [self.view addSubview:datePicker];只需要这两行代码就能显示一个日起时刻选择框:
2、自定义日期时刻选择框的大小位置:
datePicker.frame = CGRectMake(50, 100, 350, 500);
3、日期时刻选择框的几种样式:
(1)默认:
datePicker.datePickerMode = UIDatePickerModeDateAndTime;//默认就是上面的那种形式。
(2)、
datePicker.datePickerMode = UIDatePickerModeTime;显示当前时间:
(3)、
datePicker.datePickerMode = UIDatePickerModeDate;显示当前日期:
(4)、
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
倒计时模式:
4、实时监测:
添加事件:
[datePicker addTarget:self action:@selector(datePickerChange:) forControlEvents:UIControlEventValueChanged];响应事件(只有滚动停止的时候才响应事件,滚动期间不响应):
- (void) datePickerChange:(id) sender { if ([sender isKindOfClass:[UIDatePicker class]]) { UIDatePicker * datePicker = sender; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yy/MM/dd HH:mm"]; NSString * dateString = [formatter stringFromDate:datePicker.date]; NSLog(@"%@", dateString); } }
2015-11-27 10:49:03.584 07-UIDatePicker[609:9234] 15/11/27 00:49 2015-11-27 10:49:05.851 07-UIDatePicker[609:9234] 15/11/27 02:49 2015-11-27 10:49:12.975 07-UIDatePicker[609:9234] 15/11/27 04:49 2015-11-27 10:49:15.284 07-UIDatePicker[609:9234] 15/11/27 05:49 2015-11-27 10:49:17.141 07-UIDatePicker[609:9234] 15/11/27 07:49 2015-11-27 10:49:20.855 07-UIDatePicker[609:9234] 15/11/27 14:495、手动设置日期,并且改变时是否伴随动画形式:
- (void)setDate:(NSDate *)date animated:(BOOL)animated; // if animated is YES, animate the wheels of time to display the new date6、设置时间间隔:
@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,最小是1,最大是30分钟
datePicker.minuteInterval = 20;
7、设置日期选择范围:
@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 @property (nullable, nonatomic, strong) NSDate *maximumDate; // default is nil设置范围为:
datePicker.minimumDate = [NSDate date]; datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 25];
此时无论怎么向下滑,日期最终也只是停在Today,不会小于Today
此时无论怎么向上滑,日期最后总是停在Tue Dec 22处,不可能大于这个日期。
8、倒计时模式:
#import "ViewController.h" @interface ViewController () { UIDatePicker * datePicker; NSTimer * timer; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; datePicker = [[UIDatePicker alloc] init]; [self.view addSubview:datePicker]; datePicker.frame = CGRectMake(50, 100, 350, 500); datePicker.datePickerMode = UIDatePickerModeCountDownTimer; datePicker.countDownDuration = 5 * 60; //初始设置为5分钟 //每隔一分钟调用一次 timer = [NSTimer timerWithTimeInterval:60 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; NSRunLoop * runLoop = [NSRunLoop currentRunLoop]; [runLoop addTimer:timer forMode:NSDefaultRunLoopMode]; } - (void) timerAction:(id) sender { NSTimeInterval now = datePicker.countDownDuration; if (0 < now) { datePicker.countDownDuration -= 60; } }
标签:
原文地址:http://blog.csdn.net/lvdezhou/article/details/50066667