如何在 Objective-C 中制作一个不断更新的 UILabel

发布于 2024-11-24 06:29:12 字数 206 浏览 5 评论 0原文

我想让 UILabel 显示计时器的当前进度。现在,为了获取当前剩余时间,我调用 [timer1 timeLeft];,它返回一个 int。通过这种方式,我可以立即更新标签一次。我可以通过什么方式不断更新标签 (mainLabel),以便它始终显示当前计时器进度,同时具有一定的资源效率?

感谢您的帮助!

I'm looking to make a UILabel display the current progress of a timer. Right now to get the current time left I call [timer1 timeLeft]; which returns an int. In this way I can update the label ONCE, at one instant. In what way can I update the label (mainLabel) constantly so that it is always displaying the current timer progress while being somewhat resource efficient?

Thanks for all your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

稚气少女 2024-12-01 06:29:12

使用以下代码作为倒计时器。

dblElapsedSeconds=0.0; //Declare this in header
tmrElapsedTime = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateElapsedTime) userInfo:nil repeats:YES]; //Declare timer variable in header

-(void)updateElapsedTime
{
    dblElapsedSeconds += 1;
    //double seconds = [[NSDate date] timeIntervalSinceDate:self.startTime];
    int hours,minutes, lseconds;
    hours = dblElapsedSeconds / 3600;
    minutes = (dblElapsedSeconds - (hours*3600)) / 60;
    lseconds = fmod(dblElapsedSeconds, 60); 
    [lblTimeElapsed setText:[NSString stringWithFormat:@"%02d:%02d",minutes, lseconds]];
}

Use following code for Countdown timer.

dblElapsedSeconds=0.0; //Declare this in header
tmrElapsedTime = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateElapsedTime) userInfo:nil repeats:YES]; //Declare timer variable in header

-(void)updateElapsedTime
{
    dblElapsedSeconds += 1;
    //double seconds = [[NSDate date] timeIntervalSinceDate:self.startTime];
    int hours,minutes, lseconds;
    hours = dblElapsedSeconds / 3600;
    minutes = (dblElapsedSeconds - (hours*3600)) / 60;
    lseconds = fmod(dblElapsedSeconds, 60); 
    [lblTimeElapsed setText:[NSString stringWithFormat:@"%02d:%02d",minutes, lseconds]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文