¿Animar texto UILabel entre dos números?
Soy nuevo en la programación de iPhone y Mac (desarrollado para Windows antes), y tengo una pregunta:
¿Cómo animo la propiedad text
de un UILabel
entre dos números, por ejemplo, de 5 to 80 ¿en un estilo sencillo? ¿Es posible con CoreAnimation
?
He estado buscando en Google durante una hora, pero no he encontrado nada que resuelva mi problema.
Lo que quiero: Animar el dinero de los usuarios para un juego simple. No se ve muy bien cuando se va de 50 to 100 o algo así, sin animación.
¿Alguien tiene una idea de cómo hacer eso?
Gracias!
5 answers
Puede utilizar las transiciones automáticas. Funciona perfectamente bien:
// 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;
Este código funciona si myLabel ya está mostrado. Si no myLabel.layer será nil y la animación no se añadirá al objeto.
En Swift 4 eso sería:
let animation: CATransition = CATransition()
animation.duration = 1.0
animation.type = kCATransitionFade
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
myLabel.layer.add(animation, forKey: "changeTextTransition")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-04 10:08:52
Funciona bien!
Objective-C
[UIView transitionWithView:self.label
duration:.5f
options:UIViewAnimationOptionCurveEaseInOut |
UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.label.text = rand() % 2 ? @"111!" : @"42";
} completion:nil];
Swift
UIView.transitionWithView(label, duration: 0.25, options: [.CurveEaseInOut, .TransitionCrossDissolve], animations: {
self.label.text = (arc4random() % 2 == 0) ? "111" : "222"
}, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-16 09:39:49
Encontré un gran motor para interpolar valores con una variedad de diferentes funciones de temporización llamadas PRTween. Instale las clases y cree algún código en estas líneas:
- (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];
}
Funciona como un regalo para mí. :)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-11 15:23:41
Si quieres que cuente hacia arriba y hacia abajo con el nuevo número empujando el número anterior (como un ticker o algo así):
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")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-17 17:55:17
En Swift 2.0, usando el método UIView.transitionWithView()
:
UIView.transitionWithView(self.payPeriodSummaryLabel,
duration: 0.2,
options: [.CurveEaseInOut, .TransitionCrossDissolve],
animations: { () -> Void in
self.label.text = "your text value"
}, completion: nil)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-25 20:25:17