如何在 macOS 上使用 NSViewRepresentable 在 SwiftUI 应用程序中使用 CAEmitterLayer

How to use CAEmitterLayer on macOS in a SwiftUI app using NSViewRepresentable

我想在基于 SwiftUI 的 macOS 应用程序中使用 CAEmitterLayer

问题:层本身是完全可见的,但它不发射任何粒子。

→ Here’s my demo project on GitHub

我基本上为自定义 class EmitterNSView: NSView 构建了一个 NSViewRepresentable 来处理发射器层本身。

final class EmitterNSView: NSView {

    private let emitterLayer: CAEmitterLayer = {
        let layer = CAEmitterLayer()
        layer.backgroundColor = NSColor.green.withAlphaComponent(0.33).cgColor
        return layer
    }()

    private let emitterCells: [CAEmitterCell] = {
        // https://developer.apple.com/documentation/quartzcore/caemitterlayer
        let cell = CAEmitterCell()
        cell.name = "someParticle"
        cell.birthRate = 10
        cell.lifetime = 5.0
        cell.velocity = 100
        cell.velocityRange = 50
        cell.emissionLongitude = 0.0
        cell.emissionRange = CGFloat.pi * 2.0
        cell.spinRange = 5
        cell.scale = 1.0
        cell.scaleRange = 0.25
        cell.alphaSpeed = 0.25
        cell.contents = NSImage(named: "whiteParticle.png")!.cgImage
        cell.color = NSColor.systemPink.cgColor
        cell.xAcceleration = 4
        cell.yAcceleration = 3
        cell.zAcceleration = 2
        return [cell]
    }()

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.wantsLayer = true
        self.layer = CALayer()
        self.layer?.autoresizingMask = [.layerWidthSizable, .layerHeightSizable]
        self.configureEmitterLayer()
        self.layer?.addSublayer(self.emitterLayer)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layout() {
        super.layout()
        self.configureEmitterLayer()
    }

    override func updateLayer() {
        super.updateLayer()
        self.configureEmitterLayer()
    }

    func configureEmitterLayer() {
        self.emitterLayer.frame = self.frame
        self.emitterLayer.autoresizingMask = [.layerHeightSizable, .layerWidthSizable]
        self.emitterLayer.masksToBounds = false
        self.emitterLayer.drawsAsynchronously = true
        self.emitterLayer.emitterMode = .points
        self.emitterLayer.birthRate = 2
        self.emitterLayer.emitterShape = CAEmitterLayerEmitterShape.line
        self.emitterLayer.emitterSize = CGSize(width: frame.width * 0.5, height: frame.height * 0.5)
        self.emitterLayer.emitterPosition = CGPoint.zero
        self.emitterLayer.renderMode = CAEmitterLayerRenderMode.additive
        self.emitterLayer.emitterCells = self.emitterCells
        self.emitterLayer.zPosition = 10
        self.emitterLayer.beginTime = CACurrentMediaTime()
        self.emitterLayer.speed = 1.5
        self.emitterLayer.emitterCells = self.emitterCells
    
    }

}

我可以清楚地看到应用程序中图层的绿色背景这一事实表明单元格有问题吗?在这一点上感到迷茫。

UIKit 中非常相似的实现工作得很好。

如何在基于 SwiftUI 的应用程序中在 macOS 上使用 CAEmitterLayer

在将 NSImage 转换为 CGImage 时使用 cgImage(forProposedRect:context:hints:)

cell.contents = NSImage(named:"whiteParticle.png").flatMap {
    return [=10=].cgImage(forProposedRect: nil, context: nil, hints: nil)
}