2
我想要動畫更改按鈕標題。例如,按鈕的標題是「one」,點擊該按鈕後,淡出「one」,然後淡入「NewTitle」。如何動畫更改按鈕標題?
我想要動畫更改按鈕標題。例如,按鈕的標題是「one」,點擊該按鈕後,淡出「one」,然後淡入「NewTitle」。如何動畫更改按鈕標題?
比具有重複的觀點玩弄相反,簡單的淡入淡出轉變可以與CATransition
類製成。
// CATransition defaults to fade
CATransition *fade = [CATransition animation];
// fade.duration = ...
[button.layer addAnimation:fade];
[button setTitle:@"New title" forControlState:UIControlStateNormal];
該按鈕將淡入其新狀態。這適用於標籤,整個視圖層次結構,無論如何。
所以創建兩個按鈕是不是一個好主意,所以我創建了一個簡單的項目來測試代碼,你的問題,這裏是我想出
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
視圖 - 控制。米
#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
相同的方法適用於UILabels:http://stackoverflow.com/a/6267259/774 – cbowns