3

CAEmitterCell发射 2 到 3 分钟后自动停止发射。所以我的问题是我怎样才能设置CAEmitterCell它会发出无限的光。有没有办法连续发射CAEmitterCell或 是否有任何属性可CAEmitterLayer用于无限发射一个单元格?

下面是我在项目中设置的代码。

@objc func launchFireworks(_ currentView: UIView) {

    var emitterLayer = CAEmitterLayer()

    emitterLayer = CAEmitterLayer()

    // Add a layer that emits, animates, and renders a particle system.

    let size = currentView.bounds.size
    emitterLayer.emitterPosition = CGPoint(x: size.width / 2, y: size.height - 700)
    emitterLayer.renderMode = kCAEmitterLayerAdditive
    emitterLayer.emitterSize = size
    emitterLayer.emitterShape = kCAEmitterLayerRectangle

    // Get particle image
    let particleImage = UIImage(named: "particle")?.cgImage
    // The definition of a particle (launch point of the firework)

    let baseCell1 = CAEmitterCell()
    baseCell1.color = UIColor.red.withAlphaComponent(0.8).cgColor
    baseCell1.emissionLongitude = -CGFloat.pi / 2
    baseCell1.emissionRange = CGFloat.pi / 5
    baseCell1.emissionLatitude = 0
    baseCell1.lifetime = 3.0
    baseCell1.birthRate = 1
    baseCell1.velocity = 400
    baseCell1.velocityRange = 50
    baseCell1.yAcceleration = 300
    baseCell1.redRange   = 0.5
    baseCell1.greenRange = 0.5
    baseCell1.blueRange  = 0.5
    baseCell1.alphaRange = 0.5

    let baseCell2 = CAEmitterCell()
    baseCell2.color = UIColor.green.withAlphaComponent(0.8).cgColor
    baseCell2.emissionLongitude = -CGFloat.pi / 2
    baseCell2.emissionRange = CGFloat.pi / 5
    baseCell2.emissionLatitude = 0
    baseCell2.lifetime = 3.0
    baseCell2.birthRate = 1
    baseCell2.velocity = 400
    baseCell2.velocityRange = 50
    baseCell2.yAcceleration = 300
    baseCell2.redRange   = 0.5
    baseCell2.greenRange = 0.5
    baseCell2.blueRange  = 0.5
    baseCell2.alphaRange = 0.5

    let baseCell3 = CAEmitterCell()
    baseCell3.color = UIColor(red: 109.0/255.0, green: 61.0/255.0, blue: 29.0/255.0, alpha: 1.0).cgColor
    baseCell3.emissionLongitude = -CGFloat.pi / 2
    baseCell3.emissionRange = CGFloat.pi / 5
    baseCell3.emissionLatitude = 0
    baseCell3.lifetime = 3.0
    baseCell3.birthRate = 1
    baseCell3.velocity = 400
    baseCell3.velocityRange = 50
    baseCell3.yAcceleration = 300
    baseCell3.redRange   = 0.5
    baseCell3.greenRange = 0.5
    baseCell3.blueRange  = 0.5
    baseCell3.alphaRange = 0.5

    let risingCell = CAEmitterCell()
    risingCell.contents = particleImage
    risingCell.emissionLongitude = (4 * CGFloat.pi) / 2
    risingCell.emissionRange = CGFloat.pi / 7
    risingCell.scale = 0.4
    risingCell.velocity = 200
    risingCell.birthRate = 50
    risingCell.lifetime = 2.0
    risingCell.yAcceleration = 350
    risingCell.alphaSpeed = -0.7
    risingCell.scaleSpeed = -0.1
    risingCell.scaleRange = 0.1
    risingCell.beginTime = 0.1
    risingCell.duration = 0.7

    // The definition of a particle (spark animation)

    let sparkCell = CAEmitterCell()
    sparkCell.contents = particleImage
    sparkCell.emissionRange = 2 * CGFloat.pi
    sparkCell.birthRate = 5000
    sparkCell.scale = 0.5
    sparkCell.velocity = 200
    sparkCell.lifetime = 3.0
    sparkCell.yAcceleration = 80
    sparkCell.beginTime = 1
    sparkCell.duration = 0.2
    sparkCell.alphaSpeed = -0.1
    sparkCell.scaleSpeed = -0.1

    // baseCell contains rising and spark particle with animation

    baseCell1.emitterCells = [risingCell, sparkCell]
    baseCell2.emitterCells = [risingCell, sparkCell]
    baseCell3.emitterCells = [risingCell, sparkCell]

    // Add baseCell to the emitter layer

    emitterLayer.emitterCells = [baseCell1,baseCell2,baseCell3]
    currentView.layer.addSublayer(emitterLayer)
}
4

0 回答 0