Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I reuse a CAAnimation instance by modifying it after adding it to a layer?

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?

like image 928
SaldaVonSchwartz Avatar asked Feb 20 '23 17:02

SaldaVonSchwartz


1 Answers

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.

like image 124
Rob Keniger Avatar answered Apr 29 '23 16:04

Rob Keniger