Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CAEmitterCell color property not working

I have some strange behaviour of CAEmitterCell color property or I don't understand it right.

I have a cell

let cell = CAEmitterCell()

With content of simple .png file which is drawn black

cell.contents = UIImage(named: "particle")?.cgImage

And I try to change it to green

cell.color = UIColor.green.cgColor

But it is still rendered black. I tried to change "Render as" property of this image to "Template imagr" in media asset but it has no effect.

Can anyone help me to understand what I'm doing wrong?

like image 610
pulp Avatar asked Apr 14 '17 13:04

pulp


2 Answers

Ok, the case was the color of initial image: the darker the image - the less color variation you have. So better use white images for your particle emitters %)

like image 65
pulp Avatar answered Sep 18 '22 14:09

pulp


This is simple example how you can change color of the CAEmitterCell.

class ViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        createParticles()
    }

    func createParticles() {
        let particleEmitter = CAEmitterLayer()

        particleEmitter.emitterPosition = CGPoint(x: view.center.x, y: -96)
        particleEmitter.emitterShape = kCAEmitterLayerLine
        particleEmitter.emitterSize = CGSize(width: view.frame.size.width, height: 1)

        let red = makeEmitterCell(color: UIColor.red)
        let green = makeEmitterCell(color: UIColor.green)
        let blue = makeEmitterCell(color: UIColor.blue)

        particleEmitter.emitterCells = [red, green, blue]

        view.layer.addSublayer(particleEmitter)
    }

    func makeEmitterCell(color: UIColor) -> CAEmitterCell {
        let cell = CAEmitterCell()
        cell.birthRate = 3
        cell.lifetime = 7.0
        cell.lifetimeRange = 0
        cell.color = color.cgColor
        cell.velocity = 200
        cell.velocityRange = 50
        cell.emissionLongitude = CGFloat.pi
        cell.emissionRange = CGFloat.pi / 4
        cell.spin = 2
        cell.spinRange = 3
        cell.scaleRange = 0.5
        cell.scaleSpeed = -0.05

        cell.contents = UIImage(named: "images")?.cgImage
        return cell
    }

}

And example of the animation.

enter image description here

like image 21
Oleg Gordiichuk Avatar answered Sep 20 '22 14:09

Oleg Gordiichuk