我有一个CALayer的动画阴影路径:

CABasicAnimation* shadowAnimation = [CABasicAnimation animationWithKeyPath: @"shadowPath"];
shadowAnimation.duration = duration;
shadowAnimation.timingFunction = function;
shadowAnimation.fromValue = (id) panelView.layer.shadowPath;

panelView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect: CGRectSetOriginM20 (panelView.bounds, CGPointZero) cornerRadius: cornerRadius].CGPath;
[panelView.layer addAnimation: shadowAnimation forKey: @"shadow"];
panelView.layer.shadowOpacity = 1.0;


我需要通过将CALayer的content属性设置为UIImage来使用资产重新创建相同的效果。有没有办法使bounds跟随与阴影相同的动画?

最佳答案

不,您不能使内容遵循阴影路径。

相反,您必须将阴影路径和边界一起设置为动画。这可以在动画组中完成,因此您只能在该组上配置计时功能和持续时间。

CAAnimationGroup* shadowAndBounds = [CAAnimationGroup animation];
shadowAndBounds.duration = duration;
shadowAndBounds.timingFunction = function;

CABasicAnimation* shadowAnimation = [CABasicAnimation animationWithKeyPath: @"shadowPath"];
// Set to and from value for the shadow path ...

CABasicAnimation* boundsAnimation = [CABasicAnimation animationWithKeyPath: @"bounds"];
// Set to and from value for the bounds ...

shadowAndBounds.animations = @[ shadowAnimation, boundsAnimation ];
[panelView.layer addAnimation: shadowAndBounds forKey: @"shadowAndBounds"];

09-07 13:54