Search code examples
iosswiftcalayercabasicanimation

Animate View with multiple arguments


I am trying to animate the opacity of a CALayer but the values are more than one value to begin with and one to end with.

For example: I want it to animate throw these values: 0.0, 0.7, 0.3, 1.0, 0.5, 0.0

I also want the animation to repeat with auto reverse. This is what I have for now:

let redLineAnimation = CABasicAnimation(keyPath: "opacity")
    redLineAnimation.duration = 0.4
    redLineAnimation.autoreverses = true
    redLineAnimation.fromValue = 0.0
    redLineAnimation.toValue = 1.0
    redLineAnimation.repeatCount = Float.infinity
    movingRedLineLayer.addAnimation(redLineAnimation, forKey: nil)

I am new to iOS development. What can I do? Thanks!


Solution

  • Take a look at CAKeyframeAnimation. That will give you what you want.

    And if what you are animating is a view's layer, you could use the easier-to-use UIView keyframe based animation methods. (Core Animation is pretty tricky, and the docs are spotty)