4

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

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 实际上是在制作动画的深层副本而不是增加引用计数吗?

4

1 回答 1

8

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.

于 2012-04-24T01:10:54.297 回答