如何增加 UILabel 的值?

发布于 2024-12-02 05:04:05 字数 111 浏览 2 评论 0原文

我有一个值为 0 的 UILabel 和一个值为 300 的 int。如何使用 NSTimer 让 UILabel 在 2 秒内从 0 变为 300?我希望它看起来像是逐步添加的。

提前致谢!

I have a UILabel with a value of 0, and an int with a value of 300. How do I use NSTimer to have the UILabel go from 0 to 300 in 2 seconds? I want it to look like it's being added up incrementally.

Thanks in advance!

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

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

发布评论

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

评论(3

谁人与我共长歌 2024-12-09 05:04:05

实际上NSTimer的精度大约是50-100毫秒,所以它不可能像你想要的那么平滑。但是您可以将计时器设置为 0.05 并测量从开始到现在实际经过了多少时间。使用后,

value = timePassedInSeconds*300/2; 
NSString* label = [NSString stringWithFormat:@"%d", value]; 

如果这不够流畅,请尝试使用显示链接而不是 NSTimer

Actually NSTimer precision is about 50-100 milliseconds so it can't be so smooth as you want. But you can schedule timer for 0.05 and measure how much time had really passed from the beginning. After use

value = timePassedInSeconds*300/2; 
NSString* label = [NSString stringWithFormat:@"%d", value]; 

if this won't be smooth enough try to use display link instead of NSTimer.

我三岁 2024-12-09 05:04:05

你想做什么?您是否想显示某种点/得分动画?如果你进行数学计算并按照你想要的时间间隔关闭计时器,你就可以做你想做的事,但我怀疑你会得到你想要的东西。您可能想查看 UIImageView 并向其提供一组图像以进行动画处理或通过绘画自行制作动画。

What are you trying to do? Are you trying to show some sort of point/score animation? You can do what you want if you do the math and fire off the timer at the interval you want, but I doubt you'll get what you want. You might want to look at UIImageView and feed it an array of images to animate or do the animations yourself with by painting.

勿忘心安 2024-12-09 05:04:05

我想出了一个不同的解决方案:

我知道我想要“计数”持续 3 秒...即 60*0.05...基本上计时器会调用该方法 60 次才能达到总数。

addUpAb = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(addUpTimer) userInfo:nil repeats:YES];

-(void)addUpTimer {
    int addNo = abTOT / 60;    
    abInt += addNo;

    abductionCount.text = [NSString stringWithFormat:@"%d", abInt];

    if(abInt >= abTOT) {
        [addUpAb invalidate];
        abCount.text = [NSString stringWithFormat:@"%d", abTOT];
        }
    }

I figured out a different solution:

I knew I wanted the "count up" to last 3 seconds... which is 60*0.05... basically the timer is hitting the method 60 times to get to the total amount.

addUpAb = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(addUpTimer) userInfo:nil repeats:YES];

-(void)addUpTimer {
    int addNo = abTOT / 60;    
    abInt += addNo;

    abductionCount.text = [NSString stringWithFormat:@"%d", abInt];

    if(abInt >= abTOT) {
        [addUpAb invalidate];
        abCount.text = [NSString stringWithFormat:@"%d", abTOT];
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文