2013-02-26 63 views
0

在我的应用程序中,我让用户使用UIDatePicker'sCountDownTimer设置了倒计时计时器。使用UIDatePicker在iOS中进行计时器倒计时

我有一个UILabel显示计数时间当用户从CountDownTimer中选择时间时,我想显示在CoutDownTimer中选择的用户下来的coumt,如下图所示。

enter image description here

所以我检索用户从CountDownTimer选择了与下面的代码

self.dateFormatter = [[NSDateFormatter alloc] init]; 
[self.dateFormatter setDateFormat:@"HH:mm:ss"]; 

self.sleepTimer = [NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO]; 

当到达00:00,我想触发一个事件计数的时间。

我不知道如何减少每秒钟的计数。

谢谢你的帮助。

+0

您需要'NSTimer'。 http://stackoverflow.com/questions/1449035/how-do-i-use-nstimer – graver 2013-02-26 13:28:36

+0

是的,我知道,但我不知道如何减少1每秒 – 2013-02-26 13:29:20

+0

你需要一个变量来减少每次计时器火灾直到它达到0. – graver 2013-02-26 13:30:07

回答

2

保存您UIDatePicker(该值以秒为单位)中的一个实例变量称为有点像countDownDuration财产secondsLeft

self.sleepTimer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES]; 

这会每秒钟调用您的方法timerFired:。用012减去secondsLeft 1.用剩余时间更新你的UILabel(在格式化时将你的秒数格式化为hh:mm:ss - See this question)。验证倒计时是否有剩余时间,并在倒计时完成时采取适当的措施(secondsLeft <= 0)。

一旦倒计时完成,请记得清理;

[self.sleepTimer invalidate]; 
self.sleepTimer = nil; 
+0

非常感谢兄弟。我知道了。 :) – 2013-02-26 14:11:06

2

你可以做到这一点:

[NSTimer timerWithTimeInterval:1.00 target:self selector:@selector(decrementByOne:) userInfo:nil repeats:YES]; 
+0

我不知道如何减少NSDateFormatter.plz的帮助1。 – 2013-02-26 13:37:17

+0

我没有说dateFormatter。我说过要制作一个计数器,从每秒开始计数。然后上面的选择器会每秒调用它,并且你的计数器将被递减并且显示它。 – 2013-02-26 13:42:58

0

把你的日期,保存到一个属性,看看NSDates

– dateByAddingTimeInterval: 

所以,如果你

[self.date dateByAddingTimeInterval:-1]; 

你会减少1秒的日期。之后,使用新的计算日期更新UI。

而且你可以打包所有的代码在一个函数调用执行此功能:

performSelector:withObject:afterDelay: