2013-07-24 26 views
1

在我的Xcode項目中,我在xib上放了一個標籤。如何淡入和淡出標籤。連續

我想連續淡入標籤直到用戶點擊屏幕。發生這種情況時,我想要出現一個新的視圖。

任何人都可以建議如何做淡入/淡出?

+4

只是告訴我們你到目前爲止。 –

回答

5

您可以在循環中放入一對鏈接的動畫或每次調用一個保存鏈接動畫的函數,直到遇到用戶點擊。

通過鏈式動漫,我的意思是這樣的(可以設置動畫的持續時間,以滿足您的需要):

myLabel.alpha = 0.0; 
[UIView animateWithDuration:1.0 
         delay:0.0 
        options: UIViewAnimationCurveEaseOut 
       animations:^{ 
        myLabel.alpha = 1.0; 
       } 
       completion:^(BOOL finished){ 
         [UIView animateWithDuration:1.0 
              delay:1.0 
              options: UIViewAnimationCurveEaseOut 
           animations:^{ 
            myLabel.alpha = 0.0; 

           } 
       completion:^(BOOL finished){ 
        NSLog(@"Done!"); 
         }]; 
       }]; 

上面的代碼將先在你的標籤褪色,然後消失了出來。你可以把它放在一個函數中,然後調用它,直到遇到用戶點擊。

7

您可以使用選項對UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat來代替嵌套塊並手動重新啓動動畫,以告訴Core Animation您希望標籤連續淡入淡出。

- (void)startAnimatingLabel 
{ 
    self.label.alpha = 0; 

    [UIView animateWithDuration:1 
          delay:0 
         options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat 
        animations:^{ 
         self.label.alpha = 1; 
        } completion:nil]; 
} 

要停止運行動畫,只需將它們從標籤圖層中刪除即可。

- (IBAction)tap:(UITapGestureRecognizer *)sender 
{ 
    [self.label.layer removeAllAnimations]; 
    self.label.alpha = 0; 

    [self presentNewView]; 
} 

編輯:甲不太突然方式來完成將是從當前視圖狀態的動畫到最後一個(這將中斷電流,重複動畫)。

- (IBAction)tap:(UITapGestureRecognizer *)sender 
{ 
    [UIView animateWithDuration:1 
          delay:0 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ 
         self.label.alpha = 0; 
        } completion:^(BOOL finished){ 
         [self presentNewView]; 
        }]; 
} 
1

創建CABasicAnimation並將其添加到您的標籤:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
animation.fromValue = @(1.0f); 
animation.toValue = @(0.1f); 
animation.repeatCount = INFINITY; 
animation.duration = 0.75; 
animation.autoreverses = YES; 

[label.layer addAnimation:animation]; 

當您點擊此按鈕,只得到一個指向標,並刪除所有的動畫:

[label.layer removeAllAnimations]; 
0

嘗試此操作可讓您管理動畫重複次數,如果您將INFINITY替換爲您的重複次數

fadingLabel.alpha = 1.0; 
[UIView beginAnimations:@"fadingLabel" context:nil]; 
[UIView setAnimationDuration:4.0]; 
[UIView setAnimationRepeatCount:INFINITY]; 

fadingLabel.alpha = 0.0; 
[UIView setAnimationBeginsFromCurrentState:YES]; 

[UIView commitAnimations];