在我的应用程序中,我允许用户使用UIDatePicker's
CountDownTimer
设置倒数计时器。
我有一个UILabel
显示计数时间当用户从CountDownTimer中选择时间时,我想显示用户在CoutDownTimer中选择的时间,如下图所示。
所以我用以下代码从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时,我要触发一个事件。
我不知道如何减少每一秒的倒数。
感谢您的帮助。
最佳答案
将countDownDuration
的UIDatePicker
属性(该值以秒为单位)保存在名为secondsLeft
之类的实例变量中。
self.sleepTimer = [NSTimer scheduledTimerWithTimeInterval:1.00 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
这将每秒调用您的方法
timerFired:
。将secondsLeft
减去1.用剩余时间更新UILabel
(将秒数格式化为hh:mm:ss-格式化时为See this question)。验证倒数是否还剩时间,如果倒数完成(secondsLeft <= 0
),则采取适当的措施。倒数完成后,请记住进行清理;
[self.sleepTimer invalidate];
self.sleepTimer = nil;
关于ios - 在iOS中使用UIDatePicker倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15090219/