如何增加 UILabel 的值?
我有一个值为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上
NSTimer
的精度大约是50-100毫秒,所以它不可能像你想要的那么平滑。但是您可以将计时器设置为 0.05 并测量从开始到现在实际经过了多少时间。使用后,如果这不够流畅,请尝试使用显示链接而不是
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 useif this won't be smooth enough try to use display link instead of
NSTimer
.你想做什么?您是否想显示某种点/得分动画?如果你进行数学计算并按照你想要的时间间隔关闭计时器,你就可以做你想做的事,但我怀疑你会得到你想要的东西。您可能想查看 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.
我想出了一个不同的解决方案:
我知道我想要“计数”持续 3 秒...即 60*0.05...基本上计时器会调用该方法 60 次才能达到总数。
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.