Is it possible to pause a CAEmitterLayer?

485 Views Asked by At

I have a CAEmitterLayer instance that emits some CAEmitterCells. I'm wondering, is it possible to pause this layer such that no new CAEmitterCells are produced and the ones that have been produced remained fixed in their position on the screen? Then, when the CAEmitterLayer instance is "un-paused", the fixed CAEmitterCells on the screen start to move again.

Thanks for any help here.

EDIT

Setting:

emitterLayer.speed = 0.1

where emitterLayer is an instance of a subclass of CAEmitterLayer, just removes the layer completely from the view.

Setting:

emitterLayer.lifetime = 0.0

just stops any new emitterCells being produced but doesn't "freeze" the existing emitterCells at the current position.

1

There are 1 best solutions below

5
On BEST ANSWER

You can set the lifetime property of the CAEmitterLayer to 0, which will cause newly emitted cells to not even be rendered, but will leave already existing cells unaffected. When you want to "un-pause" your emitter layer, you can simply reset lifetime to whatever it was before the pause.

To freeze the existing cells as well, you can set speed to 0 and also add a timeOffset.

extension CAEmitterLayer {
    func pause() {
        // Freeze existing cells
        self.speed = 0
        self.timeOffset = convertTime(CACurrentMediaTime(), from: self)
        // Stop creating new cells
        self.lifetime = 0
    }
}

Then you can simply call it like emitterLayer.pause()