1

我正在努力使我的 CAEmitterLayer 对象一次只生成一个单元格。一旦细胞寿命到期,我想产生下一个,依此类推。这样做的原因是因为它们不断重叠导致它看起来很糟糕。

我尝试了苹果文档,但找不到任何有用的东西。 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
}
4

2 回答 2

2

这可以通过正确设置bithRate和来实现lifetimeBirthrate定义为每秒创建的对象lifetime数,是对象存活的秒数。

像这样的组合效果很好:

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
    }

结果

在此处输入图像描述

于 2019-01-03T18:46:35.220 回答
1

它只是细胞大小、速度、出生率和寿命的组合。这是完全独立的单元格的无聊流:

    let cell = CAEmitterCell()
    cell.birthRate = 5
    cell.lifetime = 1
    cell.velocity = 100

另一方面,你的出生率非常快,寿命也很长:

    cell.birthRate = 0.2
    cell.lifetime = 100
于 2019-01-03T18:45:17.247 回答