2009-07-21 72 views
2

我有這段代碼。在values數組的末尾,你可以看到我爲不透明度提供了0.5。但出於某種原因,當動畫停止時,它會再次閃爍,然後使視圖完全透明。那裏有什麼問題?這個CAKeyFrameAnimation爲什麼停止在零不透明狀態?

CALayer *layer = self.layer; 
CAKeyframeAnimation *blinkAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"]; 
blinkAnim.duration = 1.0; 
//blinkAnim.repeatCount = 0; 
blinkAnim.autoreverses = NO; 

// keyframe times and values 
// we want to start fully opaque, fade out, stay faded out and fade back in shortly before the end of the cycle 
blinkAnim.keyTimes = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0], 
         [NSNumber numberWithFloat:0.4], 
         [NSNumber numberWithFloat:0.6], 
         [NSNumber numberWithFloat:0.85], 
         [NSNumber numberWithFloat:1.0], nil]; 
blinkAnim.values = [NSArray arrayWithObjects: [NSNumber numberWithFloat:1.0], 
        [NSNumber numberWithFloat:1.0], 
        [NSNumber numberWithFloat:0.0], 
        [NSNumber numberWithFloat:0.0], 
        [NSNumber numberWithFloat:0.5], nil]; 
[layer addAnimation:blinkAnim forKey:nil]; 

回答

5

默認fillMode(參見here)用於CAAnimationkCAFillModeRemoved。你應該設定動畫的fillModekCAFillModeForwards,也是removedOnCompletion屬性設置爲NO(默認情況下它是YES):

blinkAnim.removedOnCompletion = NO; 
blinkAnim.fillMode = kCAFillModeForwards; 

你會發現,這些屬性是在我寫了以前的源設置問題 - 他們在那裏是有原因的。

相關問題