gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:38:22 24 4
gpt4 key购买 nike

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

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

→ Here’s my demo project on GitHub

enter image description here

我基本上为自定义类 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

最佳答案

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

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

关于swift - 如何在 macOS 上使用 NSViewRepresentable 在 SwiftUI 应用程序中使用 CAEmitterLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69646715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com