6

我将屏幕分成小块,然后为每个块设置动画以执行过渡:

    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 实例重置前一层的属性有关。

有人对如何补救这种情况有建议吗?

4

1 回答 1

6

timeOffset不是您要使用的属性,它实际上会在动画开始的时间点发生变化,而不是延迟开始。相反,您应该设置beginTime动画的。

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


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

Normal animation    | 12345678 |
Begin time          |     12345678 |
Time offset         | 5678     |
于 2012-07-24T07:03:33.057 回答