2012-08-12 133 views
6

我爲CALayer設置陰影路徑的動畫。動畫CALayer陰影路徑

框架調整正確,但陰影不縮放。

反而影子開始在最終大小CGSize(20,20)和整個動畫持有,即使我的shadowPath設置爲初始值

[CATransaction begin]; 
[CATransaction setAnimationDuration: 0]; 
[CATransaction setDisableActions: TRUE]; 
    layer.frame = CGRectMake(0,0,10,10); 
    layer.shadowPath = [UIBezierPath bezierPathWithRect:layer.bounds].CGPath; 
[CATransaction commit]; 

[CATransaction begin]; 

    [CATransaction setValue:[NSNumber numberWithFloat:10] forKey:kCATransactionAnimationDuration]; 
    layer.frame = CGRectMake(0,0,20,20); 
    layer.shadowPath = [UIBezierPath bezierPathWithRect:tile.bounds].CGPath; 

[CATransaction commit]; 

回答

9

起初,小方與陰影。

ss

當按下的按鈕,廣場和陰影做大在一起。

ss

主要的代碼如下:

[CATransaction begin]; 
[CATransaction setAnimationDuration:5.0]; 
CAMediaTimingFunction *timing = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
[CATransaction setAnimationTimingFunction:timing]; 
layer.frame = CGRectMake(0,0,100,100); 
[CATransaction commit];  

CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"]; 
shadowAnimation.duration = 5.0; 
shadowAnimation.fromValue = (id)[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 50, 50)].CGPath; 
shadowAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath; 
[layer addAnimation:shadowAnimation forKey:@"shadow"]; 

您可以下載從GitHub這個項目,只是運行它。

https://github.com/weed/p120812_CALayerShadowTest

這個問題問得非常難受! :)

+0

感謝您的努力。你能告訴我爲什麼我的解決方案不起作用嗎?爲什麼你可以爲框架設置動畫而不是陰影路徑? – prostock 2012-08-12 05:34:19

+0

影子動畫的要點是將'animationWithKeyPath'設置爲'@「shadowPath」'。這意味着動畫的對象是'shadowPath'。我也不知道這種機制,但設置'animationWithKeyPath'對於製作動畫非常重要。這個可以嗎 ? – weed 2012-08-12 05:47:56

3

想根據Weed的回答添加另一個答案。我採取了雜草的答案,並試圖將所有內容放入CATransaction中,因爲我想爲多個圖層製作動畫並確保動畫一起發生。如果你需要它的話,你們可以去。另外,我仍然不明白爲什麼你必須在CATransaction中使用fromValue和toValue。你爲什麼不能像做框架一樣做其他屬性呢?

[CATransaction begin]; 

[CATransaction setValue:[CAMediaTimingFunction 
    functionWithName:kCAMediaTimingFunctionEaseOut] 
    forKey:kCATransactionAnimationTimingFunction]; 

for (CALayer *layer in self.layers){ 
    CABasicAnimation *shadowAnimation = 
    [CABasicAnimation animationWithKeyPath:@"shadowPath"]; 

    shadowAnimation.fromValue = 
     (id)[UIBezierPath bezierPathWithRect:layer.bounds].CGPath; 
    shadowAnimation.timingFunction = 
     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
    layer.frame = rectFinal; 
    shadowAnimation.toValue = 
     (id)[UIBezierPath bezierPathWithRect:layer.bounds].CGPath; 
    layer.shadowPath = 
     [UIBezierPath bezierPathWithRect:layer.bounds].CGPath; 
    [layer addAnimation:shadowAnimation forKey:nil]; 
}   
[CATransaction commit];