【问题标题】:Timer Count Down with UIDatePicker in iOSiOS中使用UIDatePicker的定时器倒计时
【发布时间】:2013-02-26 13:23:03
【问题描述】:

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

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

感谢您的帮助。

【问题讨论】:

  • 是的,我知道,但我不知道如何每秒减少 1 个
  • 你需要一个变量在每次定时器触发时递减,直到它达到 0。
  • 兄弟,我该怎么做?请提供代码
  • datePicker.countDownDuration -= 1;

标签: ios cocoa-touch uidatepicker


【解决方案1】:

你可以这样做:

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

【讨论】:

  • 我不知道如何减少 1 以获得 NSDateFormatter.plz 帮助。
  • 我没有谈论 dateFormatter。我说做一个计数器,从开始开始每秒计数。然后上面的选择器会每秒调用一次,你的计数器会被 decrementedByOne 并显示出来。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    记录您的日期,将其保存到属性中并查看 NSDates

    – dateByAddingTimeInterval:
    

    如果你这样做了

    [self.date dateByAddingTimeInterval:-1];
    

    您将日期递减 1 秒。 之后,使用新的计算日期更新您的 UI。

    您还可以将所有代码打包在一个函数中并通过调用来执行该函数:

    performSelector:withObject:afterDelay: 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2018-04-10
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多