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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With