CAEmitter将粒子垂直放置在随机位置

时间:2018-07-13 14:56:02

标签: swift caemitterlayer caemittercell

我的generatorShape设置为一条线,它将单元格水平放置在随机的位置,但是由于某种原因,它还将单元格垂直放置在整个屏幕上的随机位置。为什么?

我只希望粒子的起始位置在屏幕顶部的x轴上。

我的放置和单元格生成如下:

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 个答案:

没有答案