动画 CALayer 隐藏
我试图在几微秒后隐藏 CALayer
,并使用 CABasicAnimation
为隐藏设置动画。
目前我正在尝试使用
[aLayer setHidden:YES];
CABasicAnimation * hideAnimation = [CABasicAnimation animationWithKeyPath:@"hidden"];
[hideAnimation setDuration:aDuration];
[hideAnimation setFromValue:[NSNumber numberWithBool:NO]];
[hideAnimation setToValue:[NSNumber numberWithBool:YES]];
[hideAnimation setBeginTime:0.09];
[hideAnimation setRemovedOnCompletion:NO];
[hideAnimation setDelegate:self];
[alayer addAnimation:hideAnimation forKey:@"hide"];
但是当我运行它时,该层会立即隐藏,而不是等待所需的开始时间。
我不确定我的 keyPath 是否为“隐藏”,但找不到任何其他选项,并且文档确实指出 CALayer
的 hidden
属性是可动画的。
实现我正在寻找的目标的正确方法是什么?
I'm trying to hide a CALayer
after a few microseconds and I'm using CABasicAnimation
to animate the hide.
At the moment I'm trying to use
[aLayer setHidden:YES];
CABasicAnimation * hideAnimation = [CABasicAnimation animationWithKeyPath:@"hidden"];
[hideAnimation setDuration:aDuration];
[hideAnimation setFromValue:[NSNumber numberWithBool:NO]];
[hideAnimation setToValue:[NSNumber numberWithBool:YES]];
[hideAnimation setBeginTime:0.09];
[hideAnimation setRemovedOnCompletion:NO];
[hideAnimation setDelegate:self];
[alayer addAnimation:hideAnimation forKey:@"hide"];
But when I run this, the layer is hidden immediately, rather than waiting for the desired beginTime.
I'm uncertain about my keyPath as "hidden" but couldn't find any other option and the documentation does state that the hidden
property of a CALayer
is animatable.
What's the correct way to achieve what I'm looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试对不透明度属性进行动画处理。从 1.0 到 0.0,您应该会得到您想要的效果。
Try animating the opacity property instead. Go from 1.0 to 0.0 and you should get the effect you want.
从 CAMediaTiming.h 中,它谈到了 beginTime 属性:
您应该使用 CACurrentMediaTime() + 所需的时间偏移。
From CAMediaTiming.h, it says about beginTime property:
You should use CACurrentMediaTime() + desired time offset.
我确信这对原始海报来说已经太晚了,但它可能对其他人有帮助。我一直在尝试做类似的事情,除了在
hidden
属性更改时使动画隐式化。正如 Tom 所说,动画opacity
在这种情况下不起作用,因为对图层隐藏属性的更改似乎立即生效(即使我使用beginTime
延迟动画>)。标准隐式操作使用淡入淡出过渡(
CATransition
,type = kCATransitionFade
),但这对整个图层进行操作,我想同时执行另一个动画,这不是兼容操作。经过多次实验,我终于注意到了上面@Kevin的评论——你好! ---这确实有效!所以我只是想把它说出来,以便未来的搜索者更容易看到解决方案:
这会将隐藏延迟到持续时间结束。将其与组中的其他属性动画组合,以便在图层消失之前看到它们的效果。 (您可以将其与不透明动画结合起来,使图层淡出,同时执行另一个动画。)
谢谢你,凯文!
I'm sure this is too late to do the original poster any good, but it may help others. I've been trying to do something similar, except to make the animation implicit when the
hidden
property is changed. As Tom says, animatingopacity
doesn't work in that case, as the change to the layer's hidden property seems to take effect right away (even if I delay the animation withbeginTime
).The standard implicit action uses a fade transition (
CATransition
,type = kCATransitionFade
), but this operates on the whole layer and I want to perform another animation at the same time, which is not a compatible operation.After much experimentation, I finally noticed @Kevin's comment above and --- hello! --- that actually works! So I just wanted to call it out so the solution is more visible to future searchers:
This delays the hiding until the end of the duration. Combine it with other property animations in a group to have their effects seen before the layer disappears. (You can combine this with an opacity animation to have the layer fade out, while performing another animation.)
Thank you, Kevin!
迅捷4
swift 4