多个CALayers动画 - 填充模式

时间:2012-07-24 06:58:42

标签: objective-c core-animation

我将屏幕分成小块,然后设置每个块的动画以执行转换:

    for (int x=0; x<number_of_x_splits; x++) {

    for (int y=0; y<number_of_y_splits; y++) {

        CGSize splitSize = CGSizeMake(screenBounds.width / number_of_x_splits, screenBounds.height / number_of_y_splits);

        CATransformLayer *transformLayer = [CATransformLayer layer];
        [transformLayer setFrame:CGRectMake(splitSize.width * x, splitSize.height * y, splitSize.width, splitSize.height)];
        [transformLayer setPosition:CGPointMake((splitSize.width * x) + splitSize.width / 2, (splitSize.height * y) + splitSize.height / 2)];

        ... adding some sublayers to transformLayer...

        CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
        [rotate setDuration:5.0];
        [rotate setFromValue:[NSNumber numberWithFloat:0]];
        [rotate setToValue:[NSNumber numberWithFloat:M_PI]];
        [rotate setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [rotate setTimeOffset:(1 / (number_of_x_splits + number_of_y_splits)) * (x+y)];
        [rotate setFillMode:kCAFillModeForwards];
        [rotate setRemovedOnCompletion:NO];
        [transformLayer addAnimation:rotate forKey:@"transform.rotation.y"];

    }
}

问题是只有链中的最后一个CALayer保持在最终位置。 我还尝试设置CALayer最终变换值:

[transformLayer setTransform:CATransform3DMakeRotation(M_PI, 0, 1, 0)];

我想这与在循环中创建另一个CALayer实例有关,重置上一层的属性。

任何人都有建议如何纠正这种情况?

1 个答案:

答案 0 :(得分:6)

timeOffset不是您想要使用的属性,它实际上会更改它将开始的动画的哪一点,而不是它开始之前的延迟。相反,您应该设置动画的beginTime

请记住,开始时间应为CACurrentMediaTime() + yourDelay


timeOffset和beginTime之间的区别可以这样说明。我知道我以前见过这个插图,我找不到它。

Normal animation    | 12345678 |
Begin time          |     12345678 |
Time offset         | 5678     |