Chaining Core Animation animations

前端 未结 6 1637
一整个雨季
一整个雨季 2020-12-02 10:22

Which is the most elegant and modular way to chain animation in a Core Animation context?

I mean to do animations that starts just when other finis

6条回答
  •  醉酒成梦
    2020-12-02 10:38

    You can also use animation grouping and use the beginTime field of the animation. Try something like this:

    CABasicAnimation *posAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    [posAnimation setFromValue:[NSNumber numberWithFloat:0.0]];
    [posAnimation setToValue:[NSNumber numberWithFloat:1.0]];
    // Here's the important part
    [posAnimation setDuration:10.0];
    [posAnimation setBeginTime:0.0];
    
    CABasicAnimation *borderWidthAnimation = [CABasicAnimation animationWithKeyPath:@"borderWidth"];
    [borderWidthAnimation setFromValue:[NSNumber numberWithFloat:0.0]];
    [borderWidthAnimation setToValue:[NSNumber numberWithFloat:1.0]];
    // Here's the important part
    [borderWidthAnimation setDuration:10.0];
    [borderWidthAnimation setBeginTime:5.0];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    [group setDuration:10.0];
    [group setAnimations:[NSArray arrayWithObjects:posAnimation, borderWidthAnimation, nil]];
    
    [layer addAnimation:group forKey:nil];
    

    Notice that the duration of the entire animation is 10 seconds. The first one starts at second 0 and the second one starts at 5 seconds.

提交回复
热议问题