CAEmitter is positioning particles vertically in random places

229 Views Asked by At

My emitterShape is set as a line, which positions the cells horizontally in random places, but for some reason, it also places the cells vertically all over my screen in random places. Why?

I only want the particle's start position to be somewhere on the x axis on the top of the screen.

My placement and cell generation is as follows:

func addConfettiToView(view:UIView){
    DispatchQueue.main.async {

        let emitter = CAEmitterLayer()
        emitter.emitterPosition = CGPoint(x: view.frame.size.width / 2, y: 0)
        emitter.emitterShape = kCAEmitterLayerLine
        emitter.emitterSize = CGSize(width: view.frame.size.width, height: 1)
        emitter.emitterCells = self.generateConfettiCells()
        view.layer.addSublayer(emitter)

    }
}

private func generateConfettiCells() -> [CAEmitterCell] {

    var cells:[CAEmitterCell] = [CAEmitterCell]()
    for index in 0..<16 {
        let cell = CAEmitterCell()
        cell.birthRate = 4.0
        cell.lifetime = 3
        cell.lifetimeRange = 0
        cell.velocity = 190
        cell.velocityRange = 0
        cell.yAcceleration = 500
        cell.emissionLongitude = CGFloat(Double.pi)
        cell.emissionRange = 0.5
        cell.spin = 3.5
        cell.spinRange = 0
        cell.color = UIColor(red: 1.0, green: 0.0, blue: 77.0/255.0, alpha: 1.0)
        cell.contents = UIImage(named: "Box")!
        cell.scaleRange = 0.25
        cell.scale = 0.1
        cell.alphaSpeed = 20.5
        cell.name = "cell\(index)"
        cells.append(cell)
    }
    return cells
}
0

There are 0 best solutions below