iosswiftcaemitterlayer

How can you make a CAEmitterLayer only spawn one cell at a time swift 4


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
}

Solution

  • 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