6

我正在为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];
4

2 回答 2

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

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

于 2012-08-12T04:06:14.133 回答
3

想根据 Weed 的回答添加另一个答案。我接受了杂草的回答并尝试将所有内容放在 CATransaction 中,因为我想为多个图层设置动画并确保动画一起发生。如果你需要的话,你们就去吧。另外,我仍然不明白为什么必须在 CATransaction 中使用 fromValue 和 toValue。为什么你不能对其他属性(如 frame.properties)做同样的事情。

[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];
于 2012-08-13T18:18:02.483 回答