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

Mar*_*ten 78 iphone animation text uilabel ios

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

我如何动画text的属性UILabel两个数字之间,例如从580在渐出风格?有可能CoreAnimation吗?我一直在Google上搜索一个小时,但我找不到解决问题的方法.我想要的是:为一个简单的游戏动画用户的钱.当它从50100或没有动画的那样时它看起来不太好.

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

谢谢!

Ced*_*rie 158

您可以使用自动转换.它工作得非常好:

// 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;
Run Code Online (Sandbox Code Playgroud)

如果已显示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")
Run Code Online (Sandbox Code Playgroud)

  • 实际上,如果你在`CATransition`的实例上将`removedOnCompletion`设置为'NO`,标签的`text`的每次更改都将被设置为动画. (14认同)
  • 只需注意一点,您必须在每次更改文本之前添加动画,然后再进行淡入淡出效果.但是,您不必每次都实例化一个新的Transition,同一个实例可以反复使用. (5认同)
  • 我试过这个(确保添加动画时图层不是nil),但文本仍然没有动画.还有其他陷阱吗? (2认同)
  • 问题是如何为数字设置动画,例如将它们增加值,而不是在两者之间以图形方式淡化.使用PRTween:https://github.com/shu223/TweenDemo/tree/7b10a8803df174791336b7f74e2c6a8e5bf5fe51/libs/PRTween (2认同)

Ant*_*nko 26

它运作良好!

Objective-C的

[UIView transitionWithView:self.label 
                  duration:.5f 
                   options:UIViewAnimationOptionCurveEaseInOut | 
                           UIViewAnimationOptionTransitionCrossDissolve 
                animations:^{

    self.label.text = rand() % 2 ? @"111!" : @"42";

} completion:nil];
Run Code Online (Sandbox Code Playgroud)

迅速

UIView.transitionWithView(label, duration: 0.25, options: [.CurveEaseInOut, .TransitionCrossDissolve], animations: {
    self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)


jow*_*wie 7

我发现了一个很棒的引擎,用于使用称为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];
}
Run Code Online (Sandbox Code Playgroud)

为我服务.:)


Ada*_*ite 5

如果你想让它用新的数字向上和向下计数推动前一个数字(如股票代码或其他东西):

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")
Run Code Online (Sandbox Code Playgroud)