2014-10-16 43 views
2

我有一系列想要在UILabel上執行的動畫,所以我查找了iOS7中添加的UIViewKeyframeAnimations更改UILabel的文本使它在使用UIViewKeyframeAnimations時重新出現

什麼,我想做一個簡單的版本是:

  1. 淡出的UILabel與持續時間0.5秒
  2. 等待1秒
  3. 淡入的UILabel與新文本與持續時間0.5秒

    NSTimeInterval duration = 2; 
    [UIView animateKeyframesWithDuration:duration delay:0 options:UIViewKeyframeAnimationOptionAllowUserInteraction | UIViewAnimationOptionOverrideInheritedOptions animations:^{ 
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.5/duration animations:^{ 
         self.label.alpha = 0; 
        }]; 
    
        [UIView addKeyframeWithRelativeStartTime:1.5/duration relativeDuration:0.5/duration animations:^{ 
         self.label.text = [@(arc4random_uniform(4000)) stringValue]; 
         self.label.alpha = 1; 
        }]; 
    } completion:nil]; 
    

結果是標籤文本是淡出並以新的文字重新出現,然後再次消失並再次消失。

結果的視頻:Animation Failure

乾杯 莫滕

回答

1

幽州:「其結果是,標籤文字淡出,並與新的文本重新出現,然後再次淡出和褪色再次。「。然而,我在電影中看到的是,alpha轉換正如預期的那樣工作,淡出並淡入。唯一的例外是你的行爲是在動畫開始時已經應用了文本改變,而不是在您的動畫的第二個關鍵幀開始。

瞭解addKeyframeWithRelativeStartTime塊內的代碼在animateKeyframesWithDuration被調用後立即執行就很重要。任何動畫變化(幀,阿爾法等)都會動畫。同樣,任何不是自動動畫視圖屬性(如標籤的文本更改)都將立即可見。

爲了有助於說明這一點,請嘗試在所有動畫塊(animateKeyframesWithDuration和addKeyframeWithRelativeStartTime)的開頭處放置一條NSLog語句,並在完成塊中放置一條。您會看到每個NSLog (除了完成塊中的)具有完全相同的日誌時間。

對於這個確切原因,下面的代碼工作,因爲完整的塊的執行被推遲,直到第一個動畫結束:

[UIView animateWithDuration:1 animations:^{ 
    self.label.alpha = 0; 
} completion:^(BOOL finished) { 
    self.label.text = [@(arc4random_uniform(4000)) stringValue]; 
    [UIView animateWithDuration:1 animations:^{ 
     self.label.alpha = 1; 
    }]; 
}]; 
+0

沒關係啊,所以我的選擇是1.回覆到舊animateWithDuration 2.介紹一個臨時'UILabel'和'UIImageView' 3.使用一些第三方承諾框架掃。選項1對長動畫非常混亂。 – mbogh 2014-10-16 11:56:21

0

這裏是我會做什麼:而不是使用一個簡單的動畫這樣的關鍵幀,我會用動畫與塊:

[UIView animateWithDuration:0.25 animations:^ 
{ 
    [self.label setAlpha:0.0]; 
    [self.imageview setAlpha:0.0]; 
} 
       completion: ^(BOOL completed) 
{ 

    [UIView animateWithDuration:0.25 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction animations:^ 
     { 
      self.label.text = [@(arc4random_uniform(4000)) stringValue]; 
      [self.label setAlpha:1.0]; 
     } 
         completion: ^(BOOL completed) 
     { 

     } 
     ]; 
    [UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction animations:^ 
     { 
      [self.imageview setAlpha:1.0]; 
      [self.imageview setFrame:frame]; 
     } 
         completion: ^(BOOL completed) 
     { 
      //if you need to do stuff after the animations finish, here you can do them. 
     } 
     ]; 
} 
]; 
+0

它的簡化版本,真正的情況是: 淡出'UILabel'和'UIImageView' 0.25 等待0.5 淡入'UILabel'爲新文本以0.25 在同一時間褪色和改造'的UIImageView '用1.0 – mbogh 2014-10-16 11:51:36

+0

你可以用類似的邏輯來構建你的目標。棘手的部分將與'UIImageView' imo。給我幾分鐘的時間來編碼並嘗試.. – aytunch 2014-10-16 11:56:39

+0

是的,但是嵌套使得閱讀和驗證更加困難。 – mbogh 2014-10-16 11:59:23

相關問題