1

I am trying to make it so my CAEmitterLayer object only spawn one cell at time. Once the cell lifetime expires, I want to spawn the next and so on. The reason for this is because they keep overlapping causing it to look awful.

I tried apples documentation and couldn't find anything useful. https://developer.apple.com/documentation/quartzcore/caemitterlayer

func setUpEmitter(){
    let emitter = CAEmitterLayer()
    emitter.emitterPosition = CGPoint(x: view.frame.width / 2, y: 0)
    emitter.emitterSize = CGSize(width: view.frame.width, height: 2)
    emitter.emitterShape = CAEmitterLayerEmitterShape.line
    emitter.emitterCells = generateEmitterCells()
    view.layer.insertSublayer(emitter, at: 0)
    // TutorialView.layer.addSublayer(emitter)

}

func generateEmitterCells() -> [CAEmitterCell]{
    var cells = [CAEmitterCell]()

    let cell = CAEmitterCell()
    cell.contents = UIImage(named: "startVCversion3")!.cgImage
    cell.birthRate = 0.2
    cell.lifetime = 100
    cell.velocity = CGFloat(55)
    cell.emissionLongitude = (180 * (.pi/180))
    cell.emissionRange = (45 * (.pi/180))
    cell.scale = 1
    cell.scaleRange = 1

    cells.append(cell)
    return cells
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Zack117
  • 985
  • 1
  • 13
  • 28

2 Answers2

2

This can be achieved by properly setting the bithRate and lifetime. Birthrate is defined as the number of objects created per second and lifetime is the number of seconds an object lives.

A combination such as this one works fine:

func generateEmitterCells() -> [CAEmitterCell]{
        var cells = [CAEmitterCell]()
        let cell = CAEmitterCell()
        cell.contents = UIImage(named: "Circle@3x")!.cgImage
        cell.birthRate = 0.5
        cell.lifetime = 2
        cell.velocity = CGFloat(55)
        cell.emissionLongitude = (180 * (.pi/180))
        cell.emissionRange = (45 * (.pi/180))
        cell.scale = 1
        cell.scaleRange = 1

        cells.append(cell)
        return cells
    }

Result

enter image description here

Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
1

It's just a combination of the size, velocity, birthrate and lifetime of the cells. This is a boring stream of completely separate cells:

    let cell = CAEmitterCell()
    cell.birthRate = 5
    cell.lifetime = 1
    cell.velocity = 100

You, on the other hand, have a very quick birthrate and a very long lifetime:

    cell.birthRate = 0.2
    cell.lifetime = 100
matt
  • 515,959
  • 87
  • 875
  • 1,141