2010-08-09 69 views
1

我試圖用Core Animation創建一點發光動畫。到目前爲止動畫效果很好。問題是,動畫是在表格單元格中使用的。當動畫添加到當前位於單元格中但尚未可見(無滾動)的圖層中時,動畫不會以某種方式啓動?似乎CoreAnimation不會爲當前不可見的圖層設置動畫效果?將動畫添加到不可見的圖層時CABasicAnimation未啓動

我的代碼是:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
[animation setFromValue:[NSNumber numberWithFloat:0.0]]; 
[animation setToValue:[NSNumber numberWithFloat:1.0]]; 
[animation setDuration:self.currentBlinkFrequency]; 
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; 
[animation setAutoreverses:YES]; 
[animation setRepeatCount:HUGE_VALF]; 
[[self.signalImage layer] addAnimation:animation forKey:@"opacity"];  
+0

?你得到了什麼樣的行爲,期望得到什麼? 如果您正確使用表格視圖單元格(即正確重用單元格),那麼如果單元格不可見,那麼它可能根本不存在... – Vladimir 2010-08-09 10:55:05

+0

我只想將一個發光的動畫添加到TableCell中的UIImageView。 – Chris 2010-08-09 11:24:19

回答

1

好找到一個解決辦法。我改用UIView動畫方法。因爲我的動畫速度可以在動畫中更改/關閉,所以我需要使用以下代碼來避免在同一視圖中踢出多個動畫。

此代碼triggeres動畫:

if (self.currentBlinkFrequency == 0) { 
    self.shouldContinueBlinking = FALSE; 
    self.animationIsRunning = FALSE; 
} else { 
    self.shouldContinueBlinking = TRUE; 
    if (self.animationIsRunning == FALSE) { 
     self.animationIsRunning = TRUE; 
     [self blinkAnimation:@"blink" finished:YES target:self.signalImage];    
    } 
} 

爲「blinkAnimation」 - 方法被調用的代碼anomation從另一篇文章中借用在你把你的動畫細胞計算器here

- (void)blinkAnimation:(NSString *)animationId finished:(BOOL)finished target:(UIView *)target 
{ 
    if (self.shouldContinueBlinking) { 
     [UIView beginAnimations:animationId context:target]; 
     [UIView setAnimationDuration:self.currentBlinkFrequency]; 
     [UIView setAnimationDelegate:self]; 
     [UIView setAnimationDidStopSelector:@selector(blinkAnimation:finished:target:)]; 
     if ([target alpha] == 1.0f) 
      [target setAlpha:0.0f]; 
     else 
      [target setAlpha:1.0f]; 
     [UIView commitAnimations]; 
    } 
}