动画两个数字之间的 UILabel 文本? [英] Animate UILabel text between two numbers?
问题描述
我是 iPhone 和 Mac 编程的新手(之前为 Windows 开发),我有一个问题:
I'm new to iPhone and Mac programming (developed for Windows before), and I've got a question:
如何为两个数字之间的 UILabel
的 text
属性设置动画,例如从 5 到 80 的缓出风格?CoreAnimation
可以吗?我在谷歌上搜索了一个小时,但没有找到任何解决我问题的方法.我想要的是:让用户为一个简单的游戏赚钱.当它只是从 50 到 100 或类似没有动画的东西时,它看起来不是很好.
How do I animate the text
property of an UILabel
between two numbers, e.g. from 5 to 80 in an Ease-Out style? Is it possible with CoreAnimation
?
I have been searching on Google for an hour, but I haven't found anything solving my problem.
What I want: Animate the users money for a simple game. It doesn't look very nice when it just goes from 50 to 100 or something like that without animation.
有人知道怎么做吗?
谢谢!
推荐答案
您可以使用自动过渡.它运行良好:
You can use the automatic transitions. It's working perfectly well :
// Add transition (must be called after myLabel has been displayed)
CATransition *animation = [CATransition animation];
animation.duration = 1.0;
animation.type = kCATransitionFade;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[myLabel.layer addAnimation:animation forKey:@"changeTextTransition"];
// Change the text
myLabel.text = newText;
如果 myLabel 已显示,则此代码有效.如果不是 myLabel.layer 将为 nil 并且动画将不会添加到对象中.
This code works if myLabel is already displayed. If not myLabel.layer will be nil and the animation will not be added to the object.
在 Swift 4 中:
let animation: CATransition = CATransition()
animation.duration = 1.0
animation.type = kCATransitionFade
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
myLabel.layer.add(animation, forKey: "changeTextTransition")
这篇关于动画两个数字之间的 UILabel 文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!