2014-01-15 51 views
1

我有一個開始按鈕,其初始文本「開始」與界面生成器中的操作相關聯。使用animationWithDuration更改UIButton的標題沒有任何延遲或動畫

但是,動畫會立即開始並結束,而不是持續3 + 5秒。這段代碼有什麼問題?

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UIButton *startButton; 
@end 

@implementation ViewController 

- (IBAction)startPressed:(UIButton *)sender 
{ 
    [UIView animateWithDuration:3 delay:5 
     options:UIViewAnimationOptionCurveEaseInOut 
     animations:^{ 
      [self.startButton setTitle:@"New Title" forState:UIControlStateNormal]; 
     } 
     completion:nil]; 
} 

@end 

更新:當然,這些反應是正確的。我使用了Jack Wu的建議,儘管沒有隱藏文本,setTitle使按鈕在屏幕上閃回。

- (IBAction)startPressed:(UIButton *)sender 
{ 
    [UIView animateWithDuration:1.5 delay:5 
     options:UIViewAnimationOptionCurveEaseInOut 
        animations:^{ self.startButton.alpha = 0; } 
        completion:^(BOOL c){ if (c) { 
     self.startButton.hidden = YES; 
     [self.startButton setTitle:@"newTitle" forState:UIControlStateNormal]; 
     [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut 
         animations:^{ 
          self.startButton.hidden = NO; 
          self.startButton.alpha = 1; 
         } 
         completion:nil]; 

    }}]; 
} 
+0

你確定按鈕的標題是動畫嗎? – Caleb

+0

我不確定你在這裏期待什麼...... – Jack

+0

如果你想要某種過渡,也許你可以在1.5秒內淡出按鈕,改變文字,然後再將它淡入1.5秒? – Jack

回答

4

按鈕的標題不是一個動畫屬性,所以你會看到它立即返回。這是設計。您可以在其'documentation中找到UIView的動畫屬性。以下是他們今天的情況。

@property frame 
@property bounds 
@property center 
@property transform 
@property alpha 
@property backgroundColor 
@property contentStretch 

或者,您可以將兩個子視圖標籤添加到您的按鈕並在您的動畫中淡入淡出。

1

標題不是UIButton的動畫屬性。正如其他人所建議的那樣,您需要自己製作一個動畫,方法是淡出按鈕,更改標題並將其淡出。

您也可以編寫自定義動畫代碼,將按鈕的位圖快照放置在當前按鈕的頂部,更改位圖快照下的按鈕標題,然後淡出並刪除位圖。這會給你一個褪色效果,但這將是一個相當數量的工作。