29

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.

Community
  • 1
  • 1
Rudolf Adamkovič
  • 31,030
  • 13
  • 103
  • 118
  • Please check the documentation, tt's all explained at UIView reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html with a clear explanation of the `beginAnimations:context:` replacement. – A-Live Nov 05 '12 at 12:20

3 Answers3

54

Use blocks:

[UIView transitionWithView:self.flipLabelButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{

    [self.flipLabelButton setTitle:newText forState:UIControlStateNormal];

} completion:nil];
jjv360
  • 4,120
  • 3
  • 23
  • 37
  • And that's it? I thought that only properties that are documented as "animatable" can be animated this way. Or not? – Rudolf Adamkovič Nov 05 '12 at 12:27
  • if you were using the animate functions ya, but the transition functions work for everything since it applies to the entire view. – jjv360 Nov 05 '12 at 12:29
  • I've updated my question. It still doesn't animate. Any ideas? – Rudolf Adamkovič Nov 05 '12 at 12:48
  • 1
    You're using animations and not transitions... It should be `[UIView transitionWithView:button duration:animationDuration options:UIViewAnimationOptionTransitionFlipFromRight animations:^{`... – jjv360 Nov 05 '12 at 12:50
  • If you want a fade effect you could also use `UIViewAnimationOptionTransitionCrossDissolve` – jjv360 Nov 05 '12 at 12:52
33

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)
budiDino
  • 13,044
  • 8
  • 95
  • 91
8

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.

John
  • 525
  • 3
  • 14