IOS_UIDatePicker

本文介绍了如何使用Objective-C在iOS应用中实现时间倒计时功能,包括设置时间间隔、显示当前时间以及启动定时器进行倒计时。通过实例演示了时间选择器和定时器的使用方法,适用于iOS开发人员。
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

NSTimer *timer;
NSInteger seconds;


@synthesize datePicker;
@synthesize btn;
@synthesize btn2;

- (void)viewDidLoad {
[super viewDidLoad];

//btn1 

btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 500,100,30)];
[btn setTitle:@"确定1" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor grayColor]];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

//btn2



btn2 = [[UIButton alloc] initWithFrame:CGRectMake(210, 500, 100, 30)];
[btn2 setTitle:@"确定2" forState:UIControlStateNormal];
[btn2 setBackgroundColor:[UIColor grayColor]];
[btn2 addTarget:self action:@selector(click2:) forControlEvents:UIControlEventTouchUpInside];

//初始化时间空间
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0,0,0)];

/*
支持的模式
UIDatePickerModeDate 年月日
UIDatePickerModeDateAndTime 年月日,时分秒
UIDatePickerModeTime 时分
UIDatePickerModeCountDownTimer
*/
datePicker.datePickerMode = UIDatePickerModeCountDownTimer;

//时间间隔
datePicker.minuteInterval = 3;

//设置本地化格式
datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"Chinese"];



//设置时间为当前时间
[datePicker setDate:[NSDate date] animated:YES];



// [datePicker setMinimumDate:[[NSDate alloc] ini]];

NSDate *date = [NSDate date];
NSTimeInterval timeInterval = 24*60*60*7;




[datePicker setMinimumDate:date ]; //设置最小时间
[datePicker setMaximumDate:[date dateByAddingTimeInterval:timeInterval]]; //设置最大时间

//添加控件大视图
[self.view addSubview:datePicker];
[self.view addSubview:btn];
[self.view addSubview:btn2];

}


//获取当前时间并显示大UIAlertView视图中

-(void)click:(UIButton *)sender{

NSDate *date = [self.datePicker date]; //获取当前日期的时间

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //格式化日期

NSString *msg = [formatter stringFromDate:date];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"时间" message:msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];

[alert show];





}


-(void)click2:(UIButton *)sender{
seconds = self.datePicker.countDownDuration; //声音时间s
self.datePicker.enabled = NO; //禁用控件
sender.enabled = NO; //禁用按钮

NSString *time = [NSString stringWithFormat:@"你还剩下【%ld】秒",seconds];

//弹出一个alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:time delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];

//定时器

timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(time) userInfo:nil repeats:YES];





}

-(void) time{

seconds -= 60;
NSLog(@"second = %ld",seconds);
self.datePicker.countDownDuration = seconds;
if(seconds <= 0){
[timer invalidate];
[self.btn2 setEnabled:YES];
[self.datePicker setEnabled:YES];
}



}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


 



         

转载于:https://my.oschina.net/u/582870/blog/341038

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值