2

I want to animation change button title. example, the title of button is "one", after I click that button, fade out "one" and then fade in "NewTitle".

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1687717
  • 3,375
  • 7
  • 26
  • 29

2 Answers2

3

Rather than messing around with duplicate views, simple fade transitions can be made with the CATransition class.

// CATransition defaults to fade
CATransition *fade = [CATransition animation];
// fade.duration = ...
[button.layer addAnimation:fade];

[button setTitle:@"New title" forControlState:UIControlStateNormal];

The button will fade to its new state. This works for labels, entire view hierarchies, whatever.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
0

So creating two buttons was not a good Idea so I have created a simple project to test code for your question and here is what I come up with

viewcontroller.h

   #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController{

        IBOutlet UIButton *myButton;
    }


    @property(nonatomic,strong) IBOutlet UIButton *myButton;

    -(IBAction)animateFadeOutButtonTitle:(id)sender;
    -(void)animateFadeInButtonTitle;


    @end

viewcontroller.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize myButton=_myButton;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [_myButton setTitle:@"One" forState:UIControlStateNormal];



}

-(IBAction)animateFadeOutButtonTitle:(id)sender
{
    [UIView animateWithDuration:0.25 animations:^{_myButton.titleLabel.alpha = 0.0;}];

    [self performSelector:@selector(animateFadeInButtonTitle) withObject:self afterDelay:1.0];
}

-(void)animateFadeInButtonTitle;
{

    [_myButton setTitle:@"New Title" forState:UIControlStateNormal];
    [UIView animateWithDuration:2.0
                          delay:0.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{_myButton.titleLabel.alpha = 1.0;}
                     completion:nil];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.



}

@end
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82