Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, February 7, 2016

Animate UILabel text between two numbers?

Animate UILabel text between two numbers?


I'm new to iPhone and Mac programming (developed for Windows before), and I've got a question:

How do I animate the text property of a UILabel between two numbers, e.g. from 5 to 80 in an Ease-Out style? Is it possible with CoreAnimation? I was searching on google for a 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.

Anyone having an idea how to do that?

Thanks!

Answer by CedricSoubrie for Animate UILabel text between two numbers?


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. If not myLabel.layer will be nil and the animation will not be added to the object.

Answer by jowie for Animate UILabel text between two numbers?


I found a great engine for tweening values with a variety of different timing functions called 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. :)

Answer by Anton Gaenko for Animate UILabel text between two numbers?


It works well!

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

Answer by Adam Waite for Animate UILabel text between two numbers?


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

Answer by remus for Animate UILabel text between two numbers?


In Swift 2.0, using the UIView.transitionWithView() method:

UIView.transitionWithView(self.payPeriodSummaryLabel,          duration: 0.2,          options: [.CurveEaseInOut, .TransitionCrossDissolve],          animations: { () -> Void in              self.label.text = "your text value"          }, completion: nil)  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.