ios - 在iOS中使用UIDatePicker倒数计时器

标签 ios cocoa-touch uidatepicker

在我的应用程序中,我允许用户使用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时,我要触发一个事件。

我不知道如何减少每一秒的倒数。

感谢您的帮助。

最佳答案

countDownDurationUIDatePicker属性(该值以秒为单位)保存在名为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/

相关文章:

ios - 尝试在 Swift 2.0 的 UIDatePicker 中只设置时间

IOS KeyChainWrapper 存储后值为 nil

ios - 在 Swift4 中扩展泛型类

iphone - 未调用 glkView drawInRect

ios - UICollectionView - 分离 scrollViewDidScroll 和 scrollToItemAtIndexPath

ios - UIDatePicker 在模拟器和 iPhone 之间输出不同格式的日期字符串

ios - 将字符串从 SQLite 存储到 NSMutableArray

ios - 将参数传递给@selector 方法

iPhone 相当于 -doCommandBySelector :?

ios - 从 UIDatePicker 中选择的日期与保存的日期不同