我可以在将CAAnimation实例添加到图层后通过修改它来重用它吗?(can I reuse a CAAnimation instance by modifying it after adding

编程入门 行业动态 更新时间:2024-10-26 06:28:55
我可以在将CAAnimation实例添加到图层后通过修改它来重用它吗?(can I reuse a CAAnimation instance by modifying it after adding it to a layer?)

这很有趣......我认为通过这样做

CABasicAnimation* a = [CABasicAnimation animationWithKeyPath:@"opacity"]; a.fromValue = [NSNumber numberWithFloat:0.]; a.toValue = [NSNumber numberWithFloat:1.]; a.duration = .4; a.fillMode = kCAFillModeBoth; a.removedOnCompletion = NO; CGFloat timeOffset = 0; for(CALayer* layer in layers) { a.beginTime = [someCommonSuperView.layer convertTime:CACurrentMediaTime()fromLayer:nil] + timeOffset; [layer addAnimation:a forKey:nil]; timeOffset += .4; }

我实际上总是修改相同的CABasicAnimation的beginTime,只是增加它的引用计数。 因此,我不会一个接一个地逐渐消失,而是应该弄乱所有这些层的开始时间,可能导致所有这些层在最后一个时刻立刻出现。 但上面的代码实际上似乎有效,因为这些层依次淡入淡出。

那么以这种方式重用动画是否有意义? 为了不在每次传递中创建它的新实例?

addAnimation实际上是制作动画的深层副本而不是增加引用计数吗?

This is interesting... I'd think that by doing this

CABasicAnimation* a = [CABasicAnimation animationWithKeyPath:@"opacity"]; a.fromValue = [NSNumber numberWithFloat:0.]; a.toValue = [NSNumber numberWithFloat:1.]; a.duration = .4; a.fillMode = kCAFillModeBoth; a.removedOnCompletion = NO; CGFloat timeOffset = 0; for(CALayer* layer in layers) { a.beginTime = [someCommonSuperView.layer convertTime:CACurrentMediaTime()fromLayer:nil] + timeOffset; [layer addAnimation:a forKey:nil]; timeOffset += .4; }

I am actually always modifying the same CABasicAnimation's beginTime and just incrementing its reference count. So thatI wouldn't get a series of layers fading in one after the other but rather should be messing up the start time of all of them, possibly causing all of them to just show up at once at the time of the last one. But the above code actually seems to work in that the layers fade in sequentially.

So does it make sense to reuse the animation in this way? So as to not create a new instance of it on each pass?

Is addAnimation actually making a deep copy of the animation instead of increasing the reference count?

最满意答案

根据文件 :

- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key

将动画对象添加到接收器的渲染树中以获取指定的键。

参数anim :

要添加到渲染树的动画。 请注意,对象由渲染树复制,未引用。 对对象的任何后续修改都不会传播到渲染树中。

所以我认为重用相同的CAAnimation对象是安全的。

According to the docs:

- (void)addAnimation:(CAAnimation *)anim forKey:(NSString *)key

Add an animation object to the receiver’s render tree for the specified key.

Parameter anim:

The animation to be added to the render tree. Note that the object is copied by the render tree, not referenced. Any subsequent modifications to the object will not be propagated into the render tree.

So I'd say it's safe to reuse the same CAAnimation object.

更多推荐

本文发布于:2023-08-02 20:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380635.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用它   图层   它来   实例   CAAnimation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!