5

我经常在 Swift 中看到连续发射器的示例,我在 Obj-C 中通过将发射器单元的birthRates 设置为 0.0 找到了一个示例,但它似乎不起作用,所以我一定做错了什么. 在我的示例中,我可以看到 16 次将出生率设置为 0 的消息,但粒子继续无休止地流动。

@IBAction func particleBtnAction(_ sender: Any) {

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

    // perform selector after 1.5 seconds when particles start
    perform(#selector(endParticles), with: emitter, afterDelay: 1.5)

}

private func generateEmitterCells() -> [CAEmitterCell] {

    var cells:[CAEmitterCell] = [CAEmitterCell]()
    for index in 0..<16 {
        let cell = CAEmitterCell()
        cell.birthRate = 4.0
        cell.lifetime = 1.0
        cell.lifetimeRange = 0
        cell.velocity = 0.7
        cell.velocityRange = 0
        cell.emissionLongitude = CGFloat(Double.pi)
        cell.emissionRange = 0.5
        cell.spin = 3.5
        cell.spinRange = 0
        cell.scaleRange = 0.25
        cell.scale = 0.1
        cells.append(cell)
    }
    return cells
}

@objc func endParticles(emitterLayer:CAEmitterLayer) {

    for emitterCell in emitterLayer.emitterCells! {
        emitterCell.birthRate = 0.0
        print("birth rate set to 0")
    }

}
4

4 回答 4

10

CAEmitterLayer'slifetime设置为零会停止emitterCells发射任何新的:

@objc func endParticles(emitterLayer:CAEmitterLayer) {
    emitterLayer.lifetime = 0.0
}
于 2018-09-25T12:00:46.930 回答
0

当你想结束粒子时,你可以尝试设置 isHidden 属性

emitter.isHidden = true

但请注意,所有细胞都会立即消失,无论它们何时被发射,或它们的生命周期。

另一种可能性是将所有与比例相关的属性设置为 0,然后寿命和出生率将无关紧要,因为新发射的细胞将不可见。

cell.scaleSpeed = 0
cell.scaleRange = 0
cell.scale = 0
于 2020-06-29T13:19:46.173 回答
0

对我有用的是:

let emmitter = CAEmitterLayer()
let cell = makeCell()
emmitter.emitterCells = [cell]
view.layer.addSublayer(emmitter)

 DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
    emmitter.removeFromSuperlayer()
 }
于 2021-09-13T09:31:46.727 回答
0

You can use key paths to assign a name to each cell and loop through them, changing each cell's property when you want to change them:

private func generateEmitterCells() -> [CAEmitterCell] {

    var cells:[CAEmitterCell] = [CAEmitterCell]()
    for index in 0..<16 {
        let cell = CAEmitterCell()
        cell.birthRate = 4.0
        cell.lifetime = 1.0
        cell.lifetimeRange = 0
        cell.velocity = 0.7
        cell.velocityRange = 0
        cell.emissionLongitude = CGFloat(Double.pi)
        cell.emissionRange = 0.5
        cell.spin = 3.5
        cell.spinRange = 0
        cell.scaleRange = 0.25
        cell.scale = 0.1
        cell.name = "cell\(index)" // cell name
        cells.append(cell)
    }
    return cells
}

@objc func endParticles(emitterLayer:CAEmitterLayer) {

    for i in 0..<(emitterLayer.emitterCells?.count ?? 0){

        emitterLayer.setValue(0.0, forKeyPath: "emitterCells.cell\(i).birthRate")
        print("birth rate set to 0")

    }

}
于 2018-07-11T16:14:01.150 回答