iOS (Swift) Changing a property of a CAEmitterCell after it's been initialised

882 Views Asked by At

My aim is to create a fire effect using a CAEmitterLayer, whose strength can be altered via changing the value of the alphaSpeed property of its given CAEmitterCells. A smaller value of the alphaSpeed would result in a "roaring" fire, whilst a larger value would suppress the fire.

So far, I have a subclass of CAEmitterLayer called FireEmitterLayer with an initialiser given by:

convenience init(view: UIView) {
    self.init()
    emitterPosition = CGPoint(x: 0.5 * view.bounds.width, y: view.bounds.height)
    emitterSize = CGSize(width: view.bounds.width, height: 0.05 * view.bounds.height)
    renderMode = .additive
    emitterShape = .line
    emitterCells = fireEmitterCells
 }

The emitter cells are generated for an array of UIImages representing 30x30 images of flames:

private var fireEmitterCells: FireEmitterCells {
    var emitterCells = FireEmitterCells()
    for assetIdentifier in assetIdentifiers {
        let emitterCell = fireEmitterCell(for: assetIdentifier)
        emitterCells.append(emitterCell)
    }
    return emitterCells
}

Each cell is created using this method:

private func fireEmitterCell(for assetIdentifier: UIImage.AssetIdentifier) -> CAEmitterCell {
    let fireEmitterCell = CAEmitterCell()
    fireEmitterCell.contents = UIImage(assetIdentifier: assetIdentifier).resized(to: CGSize(width: 20.0, height: 20.0)).cgImage
    fireEmitterCell.alphaSpeed = -0.3
    fireEmitterCell.birthRate = fireBirthRate
    fireEmitterCell.lifetime = fireLifetime
    fireEmitterCell.lifetimeRange = 0.5
    fireEmitterCell.color = UIColor.init(red: 0.8, green: 0.4, blue: 0.2, alpha: 0.6).cgColor
    fireEmitterCell.emissionLongitude = .pi
    fireEmitterCell.velocity = 80.0
    fireEmitterCell.velocityRange = 5.0
    fireEmitterCell.emissionRange = 0.5
    fireEmitterCell.yAcceleration = -200.0
    fireEmitterCell.scaleSpeed = 0.3
    return fireEmitterCell
}

Is there a way to alter the alphaSpeed value of these cells from an instance of this subclass, called say, fireEmitterLayer within some UIViewController:

var fireEmitterLayer = FireEmitterLayer(view: view)

I've tried adding this method within the FireEmitterLayer class

private func setAlphaSpeed(_ alphaSpeed: Float) {
    guard let emitterCells = emitterCells else { return }
    for emitterCell in emitterCells {
        emitterCell.alphaSpeed = alphaSpeed
    }
}

but this doesn't work ...

Any help is appreciated :-)

1

There are 1 best solutions below

0
On

I set my cells dynamically for my emitters whenever I want using a func as follows :

func makeEmitterCell(thisEmitter : CAEmitterLayer, newColor: UIColor, priorColor: UIColor, contentImage : UIImage) -> CAEmitterCell {

    let cell = CAEmitterCell()

    cell.birthRate = lastChosenBirthRate
    cell.lifetime = lastChosenLifetime
    cell.lifetimeRange = lastChosenLifetimeRange
    //cell.color = newColor.cgColor
    cell.emissionLongitude = lastChosenEmissionLongitude
    cell.emissionLatitude = lastChosenEmissionLatitude
    cell.emissionRange = lastChosenEmissionRange
    cell.spin = lastChosenSpin
    cell.spinRange = lastChosenSpinRange
    cell.scale = lastChosenScale
    cell.scaleRange = lastChosenScaleRange
    cell.scaleSpeed = lastChosenScaleSpeed
    cell.alphaSpeed = Float(lastChosenAlphaSpeed)
    cell.alphaRange = Float(lastChosenAlphaRange)
    cell.autoreverses = lastChosenAutoReverses
    cell.isEnabled = lastChosenIsEnabled
    cell.velocity = lastChosenVelocity
    cell.velocityRange = lastChosenVelocityRange
    cell.xAcceleration = lastChosen_X_Acceleration
    cell.yAcceleration = lastChosen_Y_Acceleration
    cell.zAcceleration = 0
    cell.contents = contentImage.cgImage

    //Access the property with this key path format: @"emitterCells.<name>.<property>"
    cell.name = "myCellName"
    let animation = CABasicAnimation(keyPath: "emitterCells.myCellName.color")
    animation.fromValue = priorColor.cgColor
    animation.toValue = newColor.cgColor
    animation.duration = CFTimeInterval(cellAnimationTimeValue)

    if thisEmitter == particleEmitter_1
    {
        particleEmitter_1.add(animation, forKey: "emitterCells.myCellName.color")
    }
    if thisEmitter == particleEmitter_2
    {
        particleEmitter_2.add(animation, forKey: "emitterCells.myCellName.color")
    }

    return cell
} // ends makeEmitterCell

There's a color animation for my design at the end of that func that you get as bonus code in a way.

I keep these variables around to allow me to control the cell features on the fly ;

var lastChosenBirthRate : Float = 300
var lastChosenLifetime : Float = 3
var lastChosenLifetimeRange : Float = 0
var lastChosenEmissionLongitude : CGFloat = 2 * CGFloat.pi
var lastChosenEmissionLatitude : CGFloat = 2 * CGFloat.pi
var lastChosenEmissionRange : CGFloat = 2 * CGFloat.pi
var lastChosenSpin : CGFloat = 1
var lastChosenSpinRange : CGFloat = 0
var lastChosenScale : CGFloat = 1
var lastChosenScaleRange : CGFloat = 0
var lastChosenScaleSpeed : CGFloat = -0.3
var lastChosenAlphaSpeed : CGFloat = -0.1
var lastChosenAlphaRange : CGFloat = 0
var lastChosenAutoReverses : Bool = false
var lastChosenIsEnabled : Bool = true
var lastChosenVelocity : CGFloat = 20
var lastChosenVelocityRange : CGFloat = 0
var lastChosen_X_Acceleration : CGFloat = 0
var lastChosen_Y_Acceleration : CGFloat = 0

I make emitter cells in several places in my code. Here is one. Part of this func makes the emitter cells and sets them for the desired emitter :

func changeParticlesCellImageRightNow(thisEmitter : CAEmitterLayer, thisContentImage : UIImage, theColor : UIColor)
{
    var priorColorToPassDown = UIColor()

    if thisEmitter == particleEmitter_1
    {
        priorColorToPassDown = lastColorUsedEmitter1
    }
    if thisEmitter == particleEmitter_2
    {
        priorColorToPassDown = lastColorUsedEmitter2
    }

    let color1 = makeEmitterCell(thisEmitter: thisEmitter, newColor: theColor, priorColor: priorColorToPassDown, contentImage: thisContentImage)

    thisEmitter.emitterCells = [color1]

}  // ends changeParticlesCellImageRightNow

Here is a call to that func :

    // Force the change to occur right now, using the existing colors
    changeParticlesCellImageRightNow(thisEmitter : particleEmitter_1, thisContentImage : emitter_1_Image, theColor : lastColorUsedInGeneral)
    changeParticlesCellImageRightNow(thisEmitter : particleEmitter_2, thisContentImage : emitter_2_Image, theColor : lastColorUsedInGeneral)

And here is the emitter layer objects

let particleEmitter_1 = CAEmitterLayer()
let particleEmitter_2 = CAEmitterLayer()

You might be able to cobble together an approach in a similar vein, setting the emitter cells birthrate, velocity, velocity range, alpha speed, scale, scale range, scale speed, etc. as best fits roaring or suppressed. A lot depends on your flexibility at the top level.