动画CALayer阴影路径

时间:2012-08-12 01:21:26

标签: objective-c core-animation

我正在设置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];

2 个答案:

答案 0 :(得分: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

这个问题对我来说很难! :)

答案 1 :(得分: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];