我在视图上有一个 uilabel 和一个 uislider。我想使用滑块设置标签的时间。滑块的范围是 00:00:00 到 03:00:00。意味着 3 小时。滑块上的间隔是 0.5 分钟。还有如何显示。我希望即使应用程序关闭也能运行计时器。
问问题
13794 次
3 回答
41
首先,在您的应用程序关闭后,无法让计时器继续运行。iPhone 上根本不允许使用后台应用程序。有一些方法可以使用计时器来伪造它(在应用程序退出时保存时间戳,并根据它重新启动的时间检查它),但它不会处理在应用程序重新启动之前计时器用完的情况向上。
至于用倒计时更新 UILabel,NSTimer 可能会起作用。像这样,假设你的类中有一个 NSTimer 计时器、一个 int secondsLeft 和一个 UILabel countdownLabel:
创建计时器:
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
updateCountdown 方法:
-(void) updateCountdown {
int hours, minutes, seconds;
secondsLeft--;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
countDownlabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}
我在我的一个应用程序中做了类似的事情,但现在没有方便的代码。
于 2009-10-06T15:10:13.863 回答
2
这段代码是错误的。
timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
它应该是。
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
于 2010-11-27T19:16:49.963 回答
0
当您的应用程序确实进入后台时,您可以保持计时器运行,
正如@shawn craver 告诉你的那样,你不能这样做,但是当应用程序进入后台(“不终止”)时你可以这样做,这是一个不同的事件 applicationDidEnterBackground 并且你将需要一些多线程 GCD(大中央调度)。
请参考这个链接
于 2013-09-11T06:09:09.333 回答