动画的CALayer的shadowPath财产 [英] Animating CALayer's shadowPath property
问题描述
我知道一个的CALayer的shadowPath使用显式的动画仅仅是动画,但我还是不能得到这个工作。我怀疑,我没有通过 toValue
正确 - 我的理解这必须是一个 ID
,但物业需要CGPathRef。在 UIBezierPath
存储但这似乎没有工作。我使用下面的code测试:
I am aware that a CALayer's shadowPath is only animatable using explicit animations, however I still cannot get this to work. I suspect that I am not passing the toValue
properly - as I understand this has to be an id
, yet the property takes a CGPathRef. Storing this in a UIBezierPath
does not seem to work. I am using the following code to test:
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
theAnimation.duration = 3.0;
theAnimation.toValue = [UIBezierPath bezierPathWithRect:CGRectMake(-10.0, -10.0, 50.0, 50.0)];
[self.view.layer addAnimation:theAnimation forKey:@"animateShadowPath"];
(我用的负值,以确保阴影超出摆在它的顶部视图...层的 masksToBounds
属性设置为 NO
)。
是如何实现的shadowPath的动画?
How is animation of the shadowPath achieved?
更新
问题的近的解决。不幸的是,主要问题是有点粗心的错误...
Problem nearly solved. Unfortunately, the main problem was a somewhat careless error...
我犯的错误是将动画添加到视图控制器的根层,而不是我献给阴影层。此外,@ pe8ter是在正确的 toValue
必须是一个 CGPathRef
转换为 ID
(显然,当我因错误层试图在此之前我还没有动画)。动画可与下列code:
The mistake I made was to add the animation to the root layer of the view controller, rather than the layer I had dedicated to the shadow. Also, @pe8ter was correct in that the toValue
needs to be a CGPathRef
cast to id
(obviously when I had tried this before I still had no animation due to the wrong layer mistake). The animation works with the following code:
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"];
theAnimation.duration = 3.0;
theAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:myRect].CGPath;
[controller.shadowLayer addAnimation:theAnimation forKey:@"shadowPath"];
我AP preciate这是很难从我提供的样品code被发现。希望它还是可以使用的,以人虽然类似的情况。
I appreciate this was difficult to spot from the sample code I provided. Hopefully it can still be of use to people in a similar situation though.
然而,当我尝试并添加行
However, when I try and add the line
controller.shadowLayer.shadowPath = [UIBezierPath bezierPathWithRect:myRect].CGPath;
动画停止工作,并阴影只是跳到最终位置瞬间。文件说,与相同的密钥作为属性添加动画被改变,以覆盖创建设置属性的值时,但shadowPath不能生成隐式动画的动画隐...所以我如何获得新的属性留下来的之后的动画?
推荐答案
首先,您没有设置动画的 fromValue
。
Firstly, you did not set the animation's fromValue
.
其次,你是正确的: toValue
接受 CGPathRef
,除了它需要被转换为 ID
。做这样的事情:
Secondly, you're correct: toValue
accepts a CGPathRef
, except it needs to be cast to id
. Do something like this:
theAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:newRect].CGPath;
您还需要,如果你想改变保持动画完成后明确设置图层的 shadowPath
属性。
You'll also need to set the shadowPath
property of the layer explicitly if you want the change to remain after animation.
这篇关于动画的CALayer的shadowPath财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!