swiftui - 为什么我不能改变 CAEmitterCell 的颜色
问题描述
为什么我无法更改 CAEmitterCell 的颜色 我在 UIImage 中使用 systemName 参数,我使用在 SFSymbols 中定义的图标。我无法更改 CAEmitterCell 的颜色。我添加了 cgColor 属性,但我的代码仍然不起作用。
使用
ParticleEffectView(particleImages: ["a.circle","b.circle","c.circle"])
粒子效果视图
struct ParticleEffectView: UIViewRepresentable {
var particleImages: [String]
func makeUIView(context: Context) -> UIView {
let host = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
let particlesLayer = CAEmitterLayer()
particlesLayer.frame = CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
host.layer.insertSublayer(particlesLayer, at: 0)
host.layer.masksToBounds = true
host.insetsLayoutMarginsFromSafeArea = false
particlesLayer.backgroundColor = .none
particlesLayer.emitterShape = .rectangle
particlesLayer.emitterPosition = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2)
particlesLayer.emitterSize = CGSize(width: UIScreen.main.bounds.width * 2, height: UIScreen.main.bounds.height * 2)
particlesLayer.emitterMode = .outline
particlesLayer.renderMode = .backToFront
particlesLayer.emitterCells = prepareParticeCell(particles: particleImages, color: UIColor.orange)
return host
}
func prepareParticeCell(particles: [String], color: UIColor) -> [CAEmitterCell] {
var arrayParticleCell: [CAEmitterCell] = [CAEmitterCell]()
for particleItem in particles {
let particleCell: CAEmitterCell = CAEmitterCell()
particleCell.contents = UIImage(systemName: particleItem)?.cgImage
particleCell.name = "XO"
particleCell.birthRate = 10
particleCell.lifetime = 20.0
particleCell.lifetimeRange = 0
particleCell.velocity = 100
particleCell.velocityRange = 100 / 4
particleCell.emissionLongitude = .pi
particleCell.emissionRange = .pi / 8
particleCell.scale = 0.5
particleCell.scaleRange = 0.5 / 3
particleCell.color = color.cgColor //here
arrayParticleCell.append(particleCell)
}
return arrayParticleCell
}
func updateUIView(_ uiView: UIView, context: Context) {
uiView.insetsLayoutMarginsFromSafeArea = false
}
}
解决方案
推荐阅读
- java - Java/Spring 中用于与 Restful API 交互的工具
- javascript - 向对消息做出反应的人发送 DM - Discord JS
- javascript - 在图表后插入文本块
- javascript - VueJS 组件不更新数据
- c++ - 如何从链表中正确删除节点?
- python - 如何将文件从本地复制到 Dockerfile 以在构建 ID 中运行
- json - python:更新列表中不同索引处的相同字典键
- tensorflow - Onnx 格式,它是如何工作的?
- ruby-on-rails - 在 Rails 5.x 中使用 DelayedJob 队列扩展 ActiveJob
- android - “无法解析符号‘WifiNetworkSpecifier’”