如何在 iPhone 中的 uilabel 上显示倒计时?
我在视图上有一个 uilabel 和一个 uislider 。我想使用滑块设置标签的时间。滑块的范围是 00:00:00 到 03:00:00。意味着 3 小时。滑块上的间隔是 0.5 分钟。还有如何显示。我希望即使应用程序关闭,计时器也会运行。
i have a uilabel and a uislider on a view. i want to set label's time using slider.range of slider is 00:00:00 to 03:00:00. means 3 hours.and intervel on slider is 0.5 minutes.also how to show.i want the timer runs even if application is closed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,应用程序关闭后无法保持计时器运行。 iPhone 上根本不允许使用后台应用程序。有多种方法可以使用计时器来伪造它(在应用程序退出时保存时间戳,并根据它重新启动时的时间进行检查),但它不会处理计时器在应用程序重新启动之前耗尽的情况向上。
至于用倒计时更新 UILabel,NSTimer 可能会起作用。像这样的事情,假设你的类中有一个 NSTimer 计时器、一个 int SecondsLeft 和一个 UILabel countdownLabel:
创建计时器:
updateCountdown 方法:
我在我的一个应用程序中做了类似的事情,但没有方便的代码现在。
First off, there's no way to keep the timer running after your app is closed. Background apps simply aren't allowed on the iPhone. There are ways to fake it with a timer (save a timestamp when the app exits, and check it against the time when it starts back up), but it won't handle the case where your timer runs out before the app is started back up.
As for updating the UILabel with the countdown, a NSTimer would probably work. Something like this, assuming you have a NSTimer timer, an int secondsLeft, and a UILabel countdownLabel in your class:
Create the timer:
The updateCountdown method:
I do something similar in one of my apps, but don't have the code handy right now.
这段代码是错误的。
应该是的。
This code is wrong.
It should be.
当应用程序进入后台时,您可以保持计时器运行,
正如 @shawn craver 告诉您不能这样做,但当应用程序进入后台(“不终止”)时,您可以这样做,这是一个不同的事件 applicationDidEnterBackground
这样你就需要一些多线程 GCD(中央调度)。
请参考此链接
在ios中设置计时器
you can keep your timer running when your application did enters background ,
as @shawn craver told u you cant do this but you can do this when application enters into background ("not terminates") which is a different event applicationDidEnterBackground
and with that you will need some multithreading GCD(grand central dispatch).
plase refer this link
set a timer in ios