我想要动画更改按钮标题。例如,按钮标题为“one”,点击该按钮后淡出“one”,然后淡入“NewTitle”。
答案 0 :(得分:3)
可以使用CATransition
类进行简单的淡入淡出过渡,而不是弄乱重复的视图。
// CATransition defaults to fade
CATransition *fade = [CATransition animation];
// fade.duration = ...
[button.layer addAnimation:fade];
[button setTitle:@"New title" forControlState:UIControlStateNormal];
按钮将淡入新状态。这适用于标签,整个视图层次结构等等。
答案 1 :(得分:0)
所以创建两个按钮并不是一个好主意,所以我创建了一个简单的项目来测试你的问题的代码,这就是我想出来的
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