动画 CALayer 的 shadowPath 属性 [英] Animating CALayer's shadowPath property

查看:27
本文介绍了动画 CALayer 的 shadowPath 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 CALayer 的 shadowPath 只能使用显式动画进行动画处理,但是我仍然无法使其正常工作.我怀疑我没有正确传递 toValue - 据我所知,这必须是一个 id,但该属性需要一个 CGPathRef.将其存储在 UIBezierPath 中似乎不起作用.我正在使用以下代码进行测试:

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).

(I am using minus values so as to ensure the shadow extends beyond a view that lies on top of it... the layer's masksToBounds property is set to 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 (显然当我在我仍然有由于错误的图层错误而没有动画).动画使用以下代码:

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"];

我很欣赏这很难从我提供的示例代码中发现.希望它仍然对处于类似情况的人有用.

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 无法生成隐式动画......那么我如何获取新属性留在动画之后?

the animation stops working, and the shadow just jumps to the final position instantly. Docs say to add the animation with the same key as the property being changed so as to override the implicit animation created when setting the value of the property, however shadowPath can't generate implicit animations... so how do I get the new property to stay after the animation?

推荐答案

首先,你没有设置动画的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆