【问题标题】:iOS: How do I prevent a timer from stopping when swapping screens or when screen sleeps?iOS:如何防止计时器在切换屏幕或屏幕休眠时停止?
【发布时间】:2014-10-27 14:01:54
【问题描述】:

我正在编写一个基本的 hh:mm:ss 计时器应用程序。我的计时器在开始、暂停和停止选项中都能完美运行。我的问题是当我在应用程序上交换屏幕时,计时器停止并返回到 00:00:00。

我是否需要将它保存在一个无论如何都会更新的数组中?有没有我可以实现的方法或教程可以让计时器连续计数,即使我交换屏幕、转到另一个应用程序或屏幕休眠?

任何想要实现计时器的人的代码:

//==================开始工作计时器=======================//

//Work Timer
- (void)updateWorkTimer {

if(working == false) return;

//calculate elapsed time
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval elapsed = workSecondsAlreadyRun + currentTime - startWorkTime;

// extract out the minutes, seconds, and hours of seconds from elapsed time:
int hours = (int)(mins / 60.0);
elapsed -= hours * 60;
int workingMins = (int)(elapsed / 60.0);
elapsed -= workingMins * 60;
int secs = (int) (elapsed);

//update our label using the format of 00:00:00
self.workTimerLabel.text = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, workingMins, secs];

//call updateTime every 1 second
[self performSelector:@selector(updateWorkTimer) withObject:self afterDelay:1];

}

//=============================================== ==================================================== ===================

- (IBAction)startWorkButton:(id)sender {

if(working == false) {

    //start timer
    working = true;
    startWorkDate = [[NSDate alloc] init];
    startWorkTime = [NSDate timeIntervalSinceReferenceDate];

    //Set color and title for the button
    [self.startWorkButton setBackgroundColor:[UIColor blueColor]];
    [sender setTitle:@"Pause Work" forState:UIControlStateNormal];
    [self updateWorkTimer];
}
else {
    //pause timer
    workSecondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startWorkDate];
    startWorkDate = [[NSDate alloc] init];

    //Set color and title for the button
    UIColor * color = [UIColor colorWithRed:56/255.0f green:171/255.0f blue:10/255.0f alpha:1.0f];
    [self.startWorkButton setBackgroundColor: color ];
    [sender setTitle:@"Resume Work" forState:UIControlStateNormal];
    working = false;
}


}

提前致谢。

【问题讨论】:

  • 也附上你的计时器代码..
  • @raki 我已附上。这对可能的解决方案有帮助吗?

标签: ios iphone xcode ipad timer


【解决方案1】:

您不需要让计时器一直运行。相反,您可以简单地计算应用再次打开后的时间间隔(应用进入后台后经过的时间),然后将其添加到计时器的值中。

为此,您需要在应用程序进入后台时使用 NSUserDefaults 存储计时器的当前值和当前时间 ([NSDate date]),并使用此数据计算应用程序再次启动后计时器应具有的值.要拦截进入后台和前台,您应该订阅UIApplicationDidEnterBackgroundNotificationUIApplicationDidBecomeActiveNotification,因为不会调用viewDidAppearviewDidDisappear

【讨论】:

  • 主要问题是屏幕的交换。如果我移动到另一个屏幕,我需要计时器继续。 UIApplicationDidEnterBackgroundNotification 没有太大帮助,因为它只显示我知道发生的操作的通知,比如离开屏幕(除非我不知道其他使用它的方式)。更改视图后重新进入视图时是否必须存储和调用时间,或者我是否可以实施一种更简单的方法,只需将时间间隔添加到离开前的时间?
  • 你知道NSNotificationCenter怎么用吗?它不显示通知,但它使您可以在某些事件发生时调用函数。我认为,存储计时器的当前值和视图控制器消失时的当前时间(在viewDidDisappear 和调用UIApplicationDidEnterBackgroundNotification 时),然后在viewDidAppearUIApplicationDidEnterBackgroundNotification 中更新计时器,仍然是最好的方法.
  • 好的,试试看。我只知道如何将它用于通知而不是实现其他方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
相关资源
最近更新 更多