ChatGPT解决这个技术问题 Extra ChatGPT

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

我是 iPhone 和 Mac 编程的新手(之前为 Windows 开发过),我有一个问题:

如何在两个数字之间为 UILabeltext 属性设置动画,例如以 Ease-Out 样式从 580CoreAnimation可以吗?我在 Google 上搜索了一个小时,但没有找到任何解决我问题的方法。我想要什么:让用户为一个简单的游戏赚钱。当它只是从 50100 或者没有动画的情况下,它看起来不太好。

任何人都知道如何做到这一点?

谢谢!

这是一个老问题,但 Pop(Facebook 动画框架)将是一个很好的解决方案。您可以直接为 text 属性设置动画。
Animate text change in UILabel 的可能重复项

A
Andrea Gottardo

您可以使用自动转换。它运行良好:

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

我试过这个(确保添加动画时图层不为零),但文本仍然没有动画。还有其他陷阱吗?
顺便说一句,每次您想要更改文本并产生淡入淡出效果时,您都必须在更改文本之前添加动画。但是,您不必每次都实例化一个新的转换,同一个实例可以一遍又一遍地重复使用。
问题是如何为数字设置动画,例如增加它们的值,而不是在两者之间以图形方式淡化。使用 PRTween:github.com/shu223/TweenDemo/tree/…
实际上,如果您在 CATransition 的实例上将 removedOnCompletion 设置为 NO,则对标签 text 的每次更改都会被动画化。
@kientux 键 changeTextTransition 没有什么特别之处。它只是 a string that identifies the animation,这意味着它是一个字符串,您可以稍后使用它来标识您之前添加的动画。如果您以后不需要能够识别您的动画,您只需指定 nil
K
Karen Hovhannisyan

它运作良好!

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)

也适用于其他属性,例如更改 UIImageViewimage
j
jowie

我发现了一个很棒的引擎,用于补间值,它具有各种不同的计时函数,称为 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];
}

对我来说是一种享受。 :)


A
Adam Waite

如果您希望它随着新数字将前一个数字推开(如股票行情或其他东西)向上和向下计数:

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

嘿!感谢您的回答!这很整洁。我只有进一步的问题。你知道如何只为文本的一部分设置动画吗?喜欢第三个字符,让其余的保持不变?我想实现一个显示数字的标签。如果值改变只有数字应该动画,那实际上改变了。
就我个人而言,我会考虑将 CoreGraphics 和 CoreAnimation 用于此类事情。或者你可以作弊并将几个标签串在一起?取决于你的字符串有多复杂。
b
brandonscript

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

S
Sergio

另一个简单的选择

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