这很有趣......我认为通过这样做
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.
更多推荐
发布评论