我是iPhone和Mac编程的新手(以前为Windows开发),但是我有一个问题:
如何以缓和样式为两个数字之间的text
属性设置动画UILabel
,例如从5到80?有可能CoreAnimation
吗?我已经在Google上搜索了一个小时,但没有找到任何解决我的问题的方法。我想要的:为用户制作一个简单游戏的资金。当它从50变为100或没有动画时类似的效果就不太好了。
有人知道该怎么做吗?
谢谢!
我是iPhone和Mac编程的新手(以前为Windows开发),但是我有一个问题:
如何以缓和样式为两个数字之间的text
属性设置动画UILabel
,例如从5到80?有可能CoreAnimation
吗?我已经在Google上搜索了一个小时,但没有找到任何解决我的问题的方法。我想要的:为用户制作一个简单游戏的资金。当它从50变为100或没有动画时类似的效果就不太好了。
有人知道该怎么做吗?
谢谢!
Answers:
您可以使用自动转换。运行良好:
// 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")
removedOnCompletion
为NO
,则CATransition
对标签的每次更改text
都会进行动画处理。
changeTextTransition
。它只是一个标识动画的字符串,这意味着它是一个字符串,您以后可以使用它来标识先前添加的动画。如果以后不需要识别动画,只需指定即可nil
。
效果很好!
目标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)
image
UIImageView
我发现了一个强大的引擎,可以使用称为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")
另一个简单的选择
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)))"
}
}
}
}