在两个数字之间animationUILabel文本?

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

如何在两个数字之间设置UILabel的文本property ,例如580之间的UILabel样式? CoreAnimation有可能吗? 我在谷歌search了一个小时,但我还没有发现任何解决我的问题。 我想要的:为一个简单的游戏animation用户的钱。 当它从50100或者没有animation的东西时,它看起来不是很好。

任何人有一个想法如何做到这一点?

谢谢!

您可以使用自动转换。 它工作得很好:

 // 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将是零,animation将不会被添加到对象。


Swift中这将是:

 let animation: CATransition = CATransition() animation.duration = 1.0 animation.type = kCATransitionFade animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) myLabel.layer.addAnimation(animation, forKey: "changeTextTransition") 

它运作良好!

Objective-C的

 [UIView transitionWithView:self.label duration:.5f options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionCrossDissolve animations:^{ self.label.text = rand() % 2 ? @"111!" : @"42"; } completion:nil]; 

迅速

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

我发现了一个很好的引擎,用于调用值为PRTween的各种不同的定时函数。 安装类,并沿着这些线创build一些代码:

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