How to chain different CAAnimation in an iOS application

前端 未结 5 2187
逝去的感伤
逝去的感伤 2020-12-05 01:10

I need to chain animations, CABasicAnimation or CAAnimationGroup but I don\'t know how to do it, the only that I do is that all the animation execute at the same time for th

5条回答
  •  长情又很酷
    2020-12-05 01:32

    What david suggests works fine, but I would recommend a different way.

    If all your animations are to the same layer, you can create an animation group, and make each animation have a different beginTime, where the first animation starts at beginTime 0, and each new animation starts after the total duration of the animations before.

    If your animations are on different layers, though, you can't use animation groups (all the animations in an animation group must act on the same layer.) In that case, you need to submit separate animations, each of which has a beginTime that is offset from CACurrentMediaTime(), e.g.:

    CGFloat totalDuration = 0;
    CABasicAnimation *animationOne = [CABasicAnimation animationWithKeyPath: @"alpha"];
    animationOne.beginTime = CACurrentMediaTime(); //Start instantly.
    animationOne.duration = animationOneDuration;
    ...
    //add animation to layer
    
    totalDuration += animationOneDuration;
    
    CABasicAnimation *animationTwo = [CABasicAnimation animationWithKeyPath: @"position"];
    animationTwo.beginTime = CACurrentMediaTime() + totalDuration; //Start after animation one.
    animationTwo.duration = animationTwoDuration;
    ...
    //add animation to layer
    
    totalDuration += animationTwoDuration;
    
    
    CABasicAnimation *animationThree = [CABasicAnimation animationWithKeyPath: @"position"];
    animationThree.beginTime = CACurrentMediaTime() + totalDuration; //Start after animation three.
    animationThree.duration = animationThreeDuration;
    ...
    //add animation to layer
    
    totalDuration += animationThreeDuration;
    

提交回复
热议问题