Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate UIButton's title change

Here I found how to animate UIButton's title change using now-deprecated beginAnimations:context: method:

UIBUtton title animation for iPhone

How to do the same thing using current APIs?

Update:

I've tried to use block-based animations:

NSTimeInterval animationDuration = animated ? 1.f : 0.f;
[UIView animateWithDuration:animationDuration delay:0.f options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction animations:^{
    [button setTitle:newTitle forState:UIControlStateNormal];
    [button setTitleColor:newTitleColor forState:UIControlStateNormal];
} completion:nil];

The color change is not animated.

like image 709
Rudolf Adamkovič Avatar asked Nov 05 '12 12:11

Rudolf Adamkovič


3 Answers

Use blocks:

[UIView transitionWithView:self.flipLabelButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{      [self.flipLabelButton setTitle:newText forState:UIControlStateNormal];  } completion:nil]; 
like image 132
jjv360 Avatar answered Sep 23 '22 01:09

jjv360


Swift

UIView.transition(with: button, duration: 0.5, options: .transitionCrossDissolve, animations: {   self.button.setTitle("WOW, new title", for: .normal)   self.button.setTitleColor(UIColor.red, for: .normal) }, completion: nil) 
like image 30
budiDino Avatar answered Sep 20 '22 01:09

budiDino


In addition to AnimationGroups, there are generally two kinds of animations, one is for property animation, the other is for animating something like your content,transition or anything that cannot use property to do animation. So the second solution is your choice,and you can achieve this by two ways: use CATransition:

CATransition *transition = [CATransition animation];
transition.type = kCATransitionFade;
transition.duration = 1;
[button.layer addAnimation:transition forKey:kCATransition];
[button setTitle:newTitle forState:UIControlStateNormal];
[button setTitleColor:newColor forState:UIControlStateNormal];

the other way is using a UIKit block as @jjv360 said, but you can't animate color inside it because obviously color can't be flipped.

like image 33
John Avatar answered Sep 23 '22 01:09

John