2014-04-29 53 views
0

傢伙我試圖執行一個CABasicAnimation(只是用於測試目的)繼Apple's guideCALayer的明確動畫

有一段代碼:

CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0]; 
fadeAnim.toValue = [NSNumber numberWithFloat:0.0]; 
fadeAnim.duration = 5.0; 
[theLayer addAnimation:fadeAnim forKey:@"opacity"]; 

// Change the actual data value in the layer to the final value. 
theLayer.opacity = 0.0; 

告訴我應該改變一個財產在最後。但它似乎不能正常工作(它立即改變不透明度) - 持續時間不是5(爲了更好的可見性,我將它改爲5),所以動畫不是CABasicAnimation而是隱式的。 它只適用於當我設置theLayer.opacity = 0.0;之前我將動畫添加到layer。我做錯了什麼或者它是文檔中的錯誤? P.S運行最新的XCode,iOS 7.1模擬器。

+0

在做動畫之前更改屬性,或者在動畫結束時使用動畫委託功能來完成動作。 –

+0

添加fadeAnim.removedOnCompletion = NO;和[fadeAnim setFillMode:kCAFillModeForwards];最後的不透明度應該在開始時設置。 – MDB983

回答

0

在添加動畫之前更新模型圖層。

CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
fadeAnim.fromValue = [NSNumber numberWithFloat:1.0]; 
fadeAnim.toValue = [NSNumber numberWithFloat:0.0]; 
fadeAnim.duration = 5.0; 

// Change the actual data value in the layer to the final value. 
theLayer.opacity = 0.0; 

[theLayer addAnimation:fadeAnim forKey:@"opacity"];