2

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

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

→ 这是我在 GitHub 上的演示项目

在此处输入图像描述

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

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就好了。

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

4

1 回答 1

3

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

cell.contents = NSImage(named:"whiteParticle.png").flatMap {
    return $0.cgImage(forProposedRect: nil, context: nil, hints: nil)
}
于 2021-10-20T17:05:43.943 回答