2

我想动画更改按钮标题。例如,按钮的标题是“one”,单击该按钮后,淡出“one”,然后淡入“NewTitle”。

4

2 回答 2

3

可以使用CATransition类进行简单的淡入淡出转换,而不是弄乱重复的视图。

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

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

该按钮将淡出到它的新状态。这适用于标签、整个视图层次结构等。

于 2012-12-13T14:53:00.540 回答
0

所以创建两个按钮不是一个好主意,所以我创建了一个简单的项目来测试你的问题的代码,这就是我想出的

视图控制器.h

   #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController{

        IBOutlet UIButton *myButton;
    }


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

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


    @end

视图控制器.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
于 2012-12-13T14:44:24.590 回答