如何在 iPhone 中的 uilabel 上显示倒计时?

发布于 2024-08-07 07:10:48 字数 130 浏览 4 评论 0原文

我在视图上有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三五鸿雁 2024-08-14 07:10:48

首先,应用程序关闭后无法保持计时器运行。 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];
}

我在我的一个应用程序中做了类似的事情,但没有方便的代码现在。

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:

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];

The updateCountdown method:

-(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];
}

I do something similar in one of my apps, but don't have the code handy right now.

你是年少的欢喜 2024-08-14 07:10:48

这段代码是错误的。

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];

This code is wrong.

timer = [NSTimer scheduledTimerWithInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];

It should be.

timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
太阳公公是暖光 2024-08-14 07:10:48

当应用程序进入后台时,您可以保持计时器运行,

正如 @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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文