UIDatePicker

UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期、时间和持续时长的输入。日期选取器的各列会按照指定的风格进行自动配置,这样就让开发者不必关心如何配置表盘这样的底层操作。你也可以对其进行定制,令其使用任何范围的日期。

用途:(1)根据需要选择不同的时间轮显示模式,主要用于显示时间让用户选择;
(2)当模式为UIDatePickerModeCountDownTimer,可是设置倒计时(倒计时时间放在countDownDuration属性中),但是Date Picker组件没有提供实际倒数计时的触发事件,还得使用NSTimer来实现倒计时。
1、基本属性介绍

UIDatePicker的显示模式
typedef NS_ENUM(NSInteger, UIDatePickerMode) {
    UIDatePickerModeTime, //根据区域设置显示小时、分,自己选择是否显示 AM/PM(e.g. 6 | 53 | PM)
    UIDatePickerModeDate, //根据区域设置显示月、天、年 (e.g. November | 15 | 2007)
    UIDatePickerModeDateAndTime,//根据区域设置显示日期、小时、分钟,可选的AM/PM (e.g. Wed Nov 15 | 6 | 53 | PM)
    UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
} __TVOS_PROHIBITED;

NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIDatePicker : UIControl <NSCoding>
@property (nonatomic) UIDatePickerMode datePickerMode; //设置datePicker模式,默认是UIDatePickerModeDateAndTime

@property (nullable, nonatomic, strong) NSLocale   *locale;   //设置时间显示的区域,默认是[NSLocale currentLocale]. setting nil returns to default
@property (null_resettable, nonatomic, copy)   NSCalendar *calendar; // 默认是[NSCalendar currentCalendar]. setting nil returns to default
@property (nullable, nonatomic, strong) NSTimeZone *timeZone; //默认值为零。 从日历使用当前时区或时区

@property (nonatomic, strong) NSDate *date;        // 默认是选择器创建时的当前日期。倒数计时器模式下此属性忽略。 对于该模式,选择器从0:00开始

@property (nullable, nonatomic, strong) NSDate *minimumDate; //指明最小/大的时间范围,默认nil,在countDownTimer模式下以及min>max,此属性忽略 
@property (nullable, nonatomic, strong) NSDate *maximumDate; // 默认为nil

@property (nonatomic) NSTimeInterval countDownDuration; // UIDatePickerModeCountDownTimer使用, 其他模式忽略.默认是0.0。限制是23:59 (86,399 seconds). 值是剩余的秒数每60 (剩余的秒数).


@property (nonatomic) NSInteger      minuteInterval;    // 显示分钟轮与间隔。 间隔必须均匀分为60.默认值为1. min为1,max为30
- (void)setDate:(NSDate *)date animated:(BOOL)animated; //如果动画为YES,则动画时间轮来显示新的日期
@end

2、例子

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];

    label=[[UILabel alloc]initWithFrame:CGRectMake(20, 30, self.view.frame.size.width-40, 50)];
    label.backgroundColor=[UIColor lightGrayColor];
    [self.view addSubview:label];


    NSDate *date=[NSDate date];
    label.text=[NSString stringWithFormat:@"%@",date];

    //初始化dataPiker,使用initWithFrame指明它在俯视图中的位置
    dataPiker=[[UIDatePicker alloc]initWithFrame:CGRectMake(10, SH-200, SW-20, 200)];

    //打印出所有的地区的标识,
    //NSLog(@"地区的标识:%@",[NSLocale availableLocaleIdentifiers]);

    //指定按中国地区显示,控件显示为--年--月--日
    dataPiker.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"];

    //设置dataPiker的日期模式,共四种;
dataPiker.datePickerMode=UIDatePickerModeDateAndTime;

//指定显示今天
[dataPiker setCalendar:[NSCalendar currentCalendar]];

//    dataPiker.timeZone = [NSTimeZone timeZoneWithName:@"GTM+8"]; // 设置时区,中国在东八区

//显示年按照2017(1987-2047)为界,显示前30年、后30年。但是效果是可以滑倒1982,只是弹回1987.
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:30];
    NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    [comps setYear:-30];
    NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];

    [datePicker setMaximumDate:maxDate];
    [datePicker setMinimumDate:minDate];


 //倒计时模式
 //dataPiker.datePickerMode=UIDatePickerModeCountDownTimer;

  //设置当前选中的值,以秒为单位计算720=12*60,所以第二个轮开始选中在12min上
    dataPiker.countDownDuration=720;

   //第二轮上的时间间隔是6的倍数
    dataPiker.minuteInterval=6;

  //监控dataPiker的值的变化Target-action
    [dataPiker addTarget:self action:@selector(timeget) forControlEvents:UIControlEventValueChanged];

   [self.view addSubview:dataPiker];


}


-(void)timeget{
    NSLog(@"rega");
  label.text=[NSString stringWithFormat:@"%@",dataPiker.date];
}

使用时用到的问题:
NSDate* minDate = [[NSDate alloc]initWithString:@”1900-01-01 00:00:00 -0500”];
错误:
No visible @interface for ‘NSDate’ declares the selector ‘initWithString:

方法:
NSString *currentDateString = @”2013-03-24 10:45:32”;
NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
[dateFormater setDateFormat:@”yyyy-MM-DD HH:mm:ss”];
NSDate *currentDate = [dateFormater dateFromString:currentDateString];

2、倒计时。滑动滚轮后,设置倒计时时间;点击button弹出开始倒计时,实际使用NSTimer,每60秒出发一次事件

#import "ViewController.h"

@interface ViewController ()
{

    UIDatePicker *datePicker;

    //实现倒计时
    UIButton  *startBn;
    NSTimer   *timer;
    NSInteger leftSeconds;

}
@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];

    [self initDatePicker];


}

-(void)initDatePicker{

    startBn = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-40,350, 80, 80)];
    startBn.backgroundColor = [UIColor redColor];
    [startBn addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startBn];


    //初始化UIDatePicker,initWithFrame:函数设置其位置、大小
    datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(10, 100, self.view.frame.size.width-20, 200)];

    //设置显示的格式--指定它的模式
    //datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.datePickerMode = UIDatePickerModeCountDownTimer;

    //指定按中国地区显示:年-月-日
    datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"];

    //指定显示今天
    [datePicker setCalendar:[NSCalendar currentCalendar]];

    //添加到View中
    [self.view addSubview:datePicker];

}


-(void)clicked{

    leftSeconds = datePicker.countDownDuration;
    datePicker.enabled = NO;

    NSString *message = [NSString stringWithFormat:@"开始倒计时,您还剩下%ld秒",(long)leftSeconds];

    //初始化UIAlertController
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];

    //设置确定按钮
    UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];

    //添加按钮
    [controller addAction:doneAction];

    //展示Controller
    [self presentViewController:controller animated:YES completion:nil];

    // 启用计时器,控制每隔60秒执行一次tickDown方法
    timer = [NSTimer scheduledTimerWithTimeInterval:60
                                             target:self selector:@selector(tickDown)
                                           userInfo:nil repeats:YES];
}


- (void) tickDown
{
    // 将剩余时间减少60秒
    leftSeconds -= 60;
    // 修改UIDatePicker的剩余时间
    datePicker.countDownDuration = leftSeconds;
    // 如果剩余时间小于等于0
    if(leftSeconds <= 0)
    {
        // 取消定时器
        [timer invalidate];
        // 启用UIDatePicker控件和按钮
        datePicker.enabled = YES;
        startBn.enabled = YES;
    }
}

@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的修养

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值