2013-07-03 20 views
1

林褪色我UIButton.titleLabel /出幾次這樣的:更改文本是0.0F,避免文字的動畫

[UIView beginAnimations:@"fadeOutText" context:NULL]; 
[UIView setAnimationDuration:1.0]; 
    button.titleLabel.alpha = 0.0f; 
[UIView commitAnimations]; 

button.titleLabel.text = @"Changed text"; 

[UIView beginAnimations:@"fadeInText" context:NULL]; 
[UIView setAnimationDuration:1.0]; 
    button.titleLabel.alpha = 1.0f; 
[UIView commitAnimations]; 

的事情是,我想在更改文本隱藏時間(alpha爲0.0f)。 但是當我淡入文本時,文本也是動畫。它出現/從標籤的右側移動。

有沒有辦法避免文本被動畫?

使用NSTimer來調用淡入和淡出。

謝謝!

回答

0

添加文本改變一個單獨的代碼段似乎刪除文本的動畫。

這是我如何解決它,也許有更好的解決方案?

-(void)hideText{ 
    [UIView beginAnimations:@"fadeOutText" context:NULL]; 
    [UIView setAnimationDuration:1.0]; 
     button.titleLabel.alpha = 0.0f; 
    [UIView commitAnimations]; 
    [NSTimer scheduledTimerWithTimeInterval:1.0 
            target:self 
            selector:@selector(setText) 
            userInfo:nil 
            repeats:NO]; 
} 
-(void)setText{ 
    [button setTitle:@"changedText" forState:UIControlStateNormal]; 
    [NSTimer scheduledTimerWithTimeInterval:0.1 
           target:self 
           selector:@selector(showText) 
           userInfo:nil 
           repeats:NO]; 
} 
-(void)showText{ 
    [UIView beginAnimations:@"fadeInText" context:NULL]; 
    [UIView setAnimationDuration:1.0]; 
     button.titleLabel.alpha = 1.0f; 
    [UIView commitAnimations]; 
} 
+0

問題是新標題設置了不同的幀,並且幀動畫應用於標籤本身。您的解決方法解決了這個問題,因爲動畫的上下文是按照順序分離和執行的 –

0

嘗試這樣的:

[UIView animateWithDuration:1.0 
         animations:^{ 
          button.titleLabel.alpha:0.0f; 
         } 
         completion:^(BOOL finished){ 
           button.titleLabel.text = @"Changed text"; 
         } 
]; 
[UIView animateWithDuration:1.0 
         animations:^{ 
          button.titleLabel.alpha:1.0f; 
         } 
]; 
相關問題