我是 iPhone 和 Mac 编程的新手(之前为 Windows 开发过),我有一个问题:
如何在两个数字之间为 UILabel
的 text
属性设置动画,例如以 Ease-Out 样式从 5 到 80? CoreAnimation
可以吗?我在 Google 上搜索了一个小时,但没有找到任何解决我问题的方法。我想要什么:让用户为一个简单的游戏赚钱。当它只是从 50 到 100 或者没有动画的情况下,它看起来不太好。
任何人都知道如何做到这一点?
谢谢!
您可以使用自动转换。它运行良好:
// 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 并且动画不会被添加到对象中。
在 Swift 4 中,这将是:
let animation: CATransition = CATransition()
animation.duration = 1.0
animation.type = kCATransitionFade
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
myLabel.layer.add(animation, forKey: "changeTextTransition")
它运作良好!
Objective-C
[UIView transitionWithView:self.label
duration:.5f
options:UIViewAnimationOptionCurveEaseInOut |
UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.label.text = rand() % 2 ? @"111!" : @"42";
} completion:nil];
斯威夫特 2
UIView.transitionWithView(label, duration: 0.25, options: [.CurveEaseInOut, .TransitionCrossDissolve], animations: {
self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
}, completion: nil)
斯威夫特 3, 4, 5
UIView.transition(with: label, duration: 0.25, options: [.curveEaseInOut, .transitionCrossDissolve], animations: {
self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
}, completion: nil)
UIImageView
的 image
我发现了一个很棒的引擎,用于补间值,它具有各种不同的计时函数,称为 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];
}
对我来说是一种享受。 :)
如果您希望它随着新数字将前一个数字推开(如股票行情或其他东西)向上和向下计数:
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")
在 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)
另一个简单的选择
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.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)))"
}
}
}
}
不定期副业成功案例分享
CATransition
的实例上将removedOnCompletion
设置为NO
,则对标签text
的每次更改都会被动画化。changeTextTransition
没有什么特别之处。它只是 a string that identifies the animation,这意味着它是一个字符串,您可以稍后使用它来标识您之前添加的动画。如果您以后不需要能够识别您的动画,您只需指定nil
。