0

我有一个粒子动画:

  private func createParticles() {
        // setup emitter
        let emitter = CAEmitterLayer()
        emitter.emitterSize = self.bounds.size
        emitter.emitterShape = .rectangle
        emitter.emitterMode = .surface
        emitter.emitterDepth = 1.0
        emitter.renderMode = .oldestFirst
        emitter.emitterPosition = CGPoint(x: bounds.width / 2, y: bounds.height / 2)

        layer.addSublayer(emitter)

        let particle1 = createParticle(rate: 1000, alpha: 1, scale: 0.2, velocity: 25)
        let particle2 = createParticle(rate: 200, alpha: 0.5, scale: 0.1, velocity: 10)
        let particle3 = createParticle(rate: 200, alpha: 1, scale: 0.1, velocity: 5)
        emitter.emitterCells = [particle1, particle2, particle3]
        
    }

    func createParticle(rate: Float, alpha: CGFloat, scale: CGFloat, velocity: CGFloat) -> CAEmitterCell {
        let cell = CAEmitterCell()
        cell.contents = UIImage(named: "Spark")?.cgImage
        cell.birthRate = rate
        cell.lifetime = 2
        cell.color = UIColor(white: 1, alpha: alpha).cgColor
        cell.alphaSpeed = -1/cell.lifetime
        cell.scale = scale
        cell.scaleRange = 0.2
        cell.alphaRange = 0.2
        cell.velocity = velocity
        cell.velocityRange = velocity * 0.3
        cell.emissionRange = 0.4
        return cell
    }

我希望粒子从层的边界发射并向内发射。这可能吗?

4

0 回答 0