2012-08-12 5 views
6

私はCALayerのシャドウパスにアニメートしています。CALayerのシャドウパスをアニメートする

フレームのサイズは正しく変更されますが、影の縮尺は変更されません。

代わりに影が最終的なサイズCGSize(20,20)で開始し、Iが初期値まず

[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

、ドロップシャドウを持つ小さな正方形にshadowPathを設定するにもかかわらず、アニメーション全体保持します。

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

+0

シャドウアニメーションのポイントは 'animationWithKeyPath'を' @ "shadowPath"に設定することです。つまり、アニメーションの対象は 'shadowPath'です。私はこのメカニズムも知らなかったが、 'animationWithKeyPath'の設定はアニメーションを作る上で非常に重要です。これでいい ? – weed

3

ウィードの答えに基づいて別の回答を追加したいと思った。私は雑草の答えを取って、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];