How to chain different CAAnimation in an iOS application

前端 未结 5 2188
逝去的感伤
逝去的感伤 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:23

    Here's a solution in Swift:

    var animations = [CABasicAnimation]()
    
    var animation1 = CABasicAnimation(keyPath: "key_path_1")
    // animation set up here, I've included a few properties as an example
    animation1.duration = 1.0
    animation1.fromValue = 1
    animation1.toValue = 0
    animation1.append(animation1)
    
    var animation2 = CABasicAnimation(keyPath: "key_path_2")
    animation2.duration = 1.0
    animation2.fromValue = 1
    animation2.toValue = 0
    // setting beginTime is the key to chaining these
    animation2.beginTime = 1.0
    animations.append(animation2)
    
    let group = CAAnimationGroup()
    group.duration = 2.0
    group.repeatCount = FLT_MAX
    group.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    group.animations = animations
    
    yourLayer.addAnimation(group, forKey: nil)
    

提交回复
热议问题