Search code examples
ioscore-animation

CAKeyframeAnimation on CAEmitterCell color not working


I tried everything but I can't animate the color property of a CAEmitterCell with a CAKeyframeAnimation. The strange thing is: I can animate possibly almost everything else (haven't tried everything, but it seems that way). For example: Animating the backgroundColor of the CAEmitterLayer containing the CAEmitterCell is no problem. Also animating the CAEmitterCell scale works fine.

Here is a shortened version of my code:

let cell = CAEmitterCell()
...
cell.name = "main"
emitter.emitterCells = [cell]

let keyPath = "emitterCells.main.color"
// Uncomment this to see that the keyPath works correctly:
//emitter.setValue(UIColor(red: 1, green: 0, blue: 0, alpha: 1).cgColor, forKeyPath: keyPath)
...
let backColorPath = "backgroundColor"
// Use this to see that color animation is basically possible:
//let anim2 = CAKeyframeAnimation(keyPath: backColorPath)
let anim2 = CAKeyframeAnimation(keyPath: keyPath)
anim2.beginTime = CACurrentMediaTime()
anim2.duration = 5
anim2.keyTimes = [0, 0.25, 0.5, 0.75, 1]
anim2.repeatCount = 20
anim2.values = [UIColor.red.cgColor, UIColor.blue.cgColor,
            UIColor.yellow.cgColor, UIColor.cyan.cgColor,
            UIColor.magenta.cgColor]
emitter.add(anim2, forKey: "coloring")

Has anyone any idea where I went wrong?


Solution

  • This was a bug inside the Apple libraries, they fixed it in iOS 11.