简体   繁体   English

动画两个数字之间的 UILabel 文本?

[英]Animate UILabel text between two numbers?

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, eg from 5 to 80 in an Ease-Out style?如何在两个数字之间为UILabeltext属性设置动画,例如在缓出样式中从580 Is it possible with CoreAnimation ? 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.当它从50100或类似没有动画的东西时,它看起来不是很好。

Anyone having an idea how to do that?任何人都知道如何做到这一点?

Thanks!谢谢!

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.如果 myLabel 已显示,则此代码有效。 If not myLabel.layer will be nil and the animation will not be added to the object.如果不是 myLabel.layer 将为 nil 并且动画将不会添加到对象中。


in Swift 4 that 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")

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)

I found a great engine for tweening values with a variety of different timing functions called PRTween .我找到了一个很棒的引擎,可以使用称为PRTween的各种不同的计时函数来补间值。 Install the classes and create some code along these lines:安装这些类并按照以下方式创建一些代码:

- (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.对我有用。 :) :)

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")

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)

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.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)))"
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM