How can I chain Core Animations for different Layers one after the other?

前端 未结 3 824
野的像风
野的像风 2021-02-08 15:28

I have a scrollView with paging enabled and a number N of pages, which are UIViews as subviews of the scrollView.

I\'m trying to do the following:

User scrolls t

3条回答
  •  长发绾君心
    2021-02-08 15:59

    Actually, it turns out that the key is getting the current time in terms of a frame of reference and adding any time offset to that current time. This works also for non-grouped animations.

    For instance, something along the lines of this code would cause n layers (assumed to be stored in some array) to sequentially fade in one after the other, each taking .8 secs.:

    CGFloat timeOffset = 0;
    
    [CATransaction begin];
    
    for (CALayer *layer in layers) {
        CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:@"opacity"];
    
        a.fromValue = @(0);
        a.toValue = @(1);
        a.fillMode = kCAFillModeForwards;
        a.beginTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil] + timeOffset;
        a.duration = 0.8;
        a.removedOnCompletion = NO;
    
        [layer addAnimation:a forKey:nil];
    
        timeOffset += a.duration;
    }
    
    [CATransaction commit];
    

    In the above case, the frame of reference is simply the current time when the invocations take place.

提交回复
热议问题