ios 动画两个数字之间的 UILabel 文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5301305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Animate UILabel text between two numbers?
提问by Markus Kasten
I'm new to iPhone and Mac programming (developed for Windows before), and I've got a question:
我是 iPhone 和 Mac 编程的新手(之前为 Windows 开发),我有一个问题:
How do I animate the text
property of an UILabel
between two numbers, e.g. from 5to 80in 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 50to 100or something like that without animation.
如何为两个数字之间的text
属性设置动画UILabel
,例如以缓出样式从5到80?有可能CoreAnimation
吗?我在谷歌上搜索了一个小时,但没有找到任何解决我问题的方法。我想要的是:让用户为一个简单的游戏赚钱。当它从50到100或类似没有动画的东西时,它看起来不是很好。
Anyone having an idea how to do that?
任何人都知道如何做到这一点?
Thanks!
谢谢!
回答by CedricSoubrie
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;
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.
如果 myLabel 已显示,则此代码有效。如果不是 myLabel.layer 将为 nil 并且动画将不会添加到对象中。
in Swift 4that would be:
在Swift 4 中,这将是:
let animation: CATransition = CATransition()
animation.duration = 1.0
animation.type = kCATransitionFade
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
myLabel.layer.add(animation, forKey: "changeTextTransition")
回答by Anton Gaenko
It works well!
它运作良好!
Objective-C
目标-C
[UIView transitionWithView:self.label
duration:.5f
options:UIViewAnimationOptionCurveEaseInOut |
UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.label.text = rand() % 2 ? @"111!" : @"42";
} completion:nil];
Swift 2
斯威夫特 2
UIView.transitionWithView(label, duration: 0.25, options: [.CurveEaseInOut, .TransitionCrossDissolve], animations: {
self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
}, completion: nil)
Swift 3, 4, 5
斯威夫特 3、4、5
UIView.transition(with: label, duration: 0.25, options: [.curveEaseInOut, .transitionCrossDissolve], animations: {
self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
}, completion: nil)
回答by jowie
I found a great engine for tweening values with a variety of different timing functions called PRTween. Install the classes and create some code along these lines:
我找到了一个很棒的引擎,可以使用称为PRTween的各种不同的计时函数来补间值。安装这些类并按照以下方式创建一些代码:
- (IBAction)tweenValue
{
[[PRTween sharedInstance] removeTweenOperation:activeTweenOperation];
PRTweenPeriod *period = [PRTweenPeriod periodWithStartValue:0.0 endValue:100.0 duration:1.0];
activeTweenOperation = [[PRTween sharedInstance] addTweenPeriod:period
target:self
selector:@selector(update:)
timingFunction:&PRTweenTimingFunctionCircOut];
}
- (void)update:(PRTweenPeriod*)period
{
self.animatingView.center = CGPointMake(period.tweenedValue + 100.0, 200.0);
self.valueLabel.text = [NSString stringWithFormat:@"%.2f", period.tweenedValue];
}
Works a treat for me. :)
对我有用。:)
回答by Adam Waite
If you kind of want it to count up and down with the new number pushing the previous number away (like a ticker or something):
如果您希望它用新数字将前一个数字推开(例如股票代码或其他东西)来上下计数:
let animation = CATransition()
animation.removedOnCompletion = true
animation.duration = 0.2
animation.type = kCATransitionPush
animation.subtype = newValue > value ? kCATransitionFromTop : kCATransitionFromBottom
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
valueLabel.layer.addAnimation(animation, forKey:"changeTextTransition")
回答by brandonscript
In Swift 2.0, using the UIView.transitionWithView()
method:
在 Swift 2.0 中,使用UIView.transitionWithView()
方法:
UIView.transitionWithView(self.payPeriodSummaryLabel,
duration: 0.2,
options: [.CurveEaseInOut, .TransitionCrossDissolve],
animations: { () -> Void in
self.label.text = "your text value"
}, completion: nil)
回答by Sergio
another simple alternative
另一个简单的选择
extension UILabel {
func countAnimation(upto: Double) {
let from: Double = text?.replace(string: ",", replacement: ".").components(separatedBy: CharacterSet.init(charactersIn: "-0123456789.").inverted).first.flatMap { Double(##代码##) } ?? 0.0
let steps: Int = 20
let duration = 0.350
let rate = duration / Double(steps)
let diff = upto - from
for i in 0...steps {
DispatchQueue.main.asyncAfter(deadline: .now() + rate * Double(i)) {
self.text = "\(from + diff * (Double(i) / Double(steps)))"
}
}
}
}