动画多个CALayers,但不在同一时间空间

时间:2009-02-18 23:39:37

标签: core-animation

我有几个CALayers我试图动画为新zPosition,每个图层与其他图层稍有延迟。

每个动画应该花费0.25秒并在上一个动画开始后0.05秒开始。在每个动画结束时,图层将从图层树中删除。

我已成功使用-animationDidStop:finished:委托方法删除我们完成的图层,但我无法正确订购动画。

是否可以通过这种方式安排动画,以及如何安排动画?

5 个答案:

答案 0 :(得分:17)

我仍然希望听到别人的建议,但我想我已经解决了这个问题。

我现在正在创建特定的CAAnimation个对象并指定其beginTime属性。我之前做过这个并没有用,我最终意识到,要使beginTime属性得到尊重,必须将动画添加到CAAnimationGroup

所以,我的代码看起来像这样:

NSArray *layers = /* layers to be animated away */
CGFloat startOffset = 0.01;

for (NSInteger index = 0; index < layers.count; index++) {
    CALayer *layer = [layers objectAtIndex:index];

    CABasicAnimation *zoomOut = [CABasicAnimation animationWithKeyPath:@"zPosition"];
    zoomOut.toValue = [NSNumber numberWithDouble:400.0];
    zoomOut.beginTime = index * startOffset;

    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = [NSArray arrayWithObject:zoomOut];
    group.delegate = self;

    [layer addAnimation:group forKey:@"zoomAway"];
}

答案 1 :(得分:14)

我发现如果你将它的值作为QuartzCore的CACurrentMediaTime()函数返回的值的delta作为一个组,那么BeginTime属性确实可以正常运行而不将动画放入一个组中。

e.g。 anim.beginTime = CACurrentMediaTime() + 0.05;

答案 2 :(得分:5)

我希望我有代表发表评论,但是将anim.beginTime设置为CACurrentMediaTime()的原因是其他一些文档所揭示的:

AVCoreAnimationBeginTimeAtZero 使用此常量将CoreAnimation的动画beginTime属性设置为时间0。 常量是一个很小的非零正值,可以防止CoreAnimation用CACurrentMediaTime替换0.0。 适用于iOS 4.0及更高版本。 在AVAnimation.h中声明。

因此,将beginTime正常设置为0是将其设置为CACurrentMediaTime()的简写。所以你可以用它来错开各个小组的开始。

答案 3 :(得分:1)

Swift 3

事实证明,通过执行以下操作,您可以相对简单地执行此操作:

var timeOffset:Double = 0 
let delay:Double = 0.1
for layer in layers {
    let a = CABasicAnimation(keyPath: "path"
    a.fromValue = layer.ovalPathSmall.cgPath
    a.toValue = layer.ovalPathLarge.cgPath
    a.fillMode = kCAFillModeForwards
    a.beginTime = CACurrentMediaTime() + timeOffset
    a.duration = 0.3
    a.isRemovedOnCompletion = true
    layer.add(a, forKey: nil)

    timeOffset += 0.3 + delay
} 

所有图层都是CALayer或CAShapeLayer,以防万一你想知道是什么样的ovalPathSmall和ovalPathLarge:

ovalPathSmall = UIBezierPath(arcCenter: position, radius: smallRadius, startAngle: 0, endAngle: 2 * .pi, clockwise: true)
ovalPathLarge = UIBezierPath(arcCenter: position, radius: largeRadius, startAngle: 0, endAngle: 2 * .pi, clockwise: true)

答案 4 :(得分:0)

感谢您分享您的结果,我还发现如果没有组使用,BeginTime属性不起作用。

在我的情况下,如果在CABasicAnimation上设置,则会忽略某些设置(如BeginTime和Duration),但如果直接设置为CAAnimationGroup,则会有效。

相关问题