我在互联网上寻找过这个问题,但找不到与我的问题相关的任何解决方案。
这是我到目前为止编写的代码。
创建 CAEmitterLayer 如下:
func createParticles() {
let particleEmitter = CAEmitterLayer()
particleEmitter.emitterPosition = CGPoint(x: self.confettiOuterView.frame.width / 2, y: 0)
particleEmitter.emitterShape = .line
particleEmitter.emitterSize = CGSize(width: self.confettiOuterView.frame.size.width, height: 1)
let red = makeEmitterCell(color: .celebration_color_01) // custom color
let green = makeEmitterCell(color: .celebration_color_02) // custom color
let blue = makeEmitterCell(color: .celebration_color_03) // custom color
particleEmitter.emitterCells = [red, green, blue]
self.confettiOuterView.layer.addSublayer(particleEmitter)
}
创建每个 CAEmitterCell 如下:
func makeEmitterCell(color: UIColor) -> CAEmitterCell {
let cell = CAEmitterCell()
cell.color = color.cgColor // this thing is not working I think
cell.birthRate = 30
cell.lifetime = 1.0
cell.lifetimeRange = 2.0
cell.velocity = 200
cell.velocityRange = 100
cell.emissionLongitude = CGFloat.pi
cell.emissionRange = CGFloat.pi / 4
cell.spin = 2
cell.spinRange = 5
cell.scale = 0.2
cell.scaleRange = 0.1
cell.contents = UIImage(named: "rate-us")?.cgImage
return cell
}
我正在使用的图片来自此链接:https ://icons8.com/icon/10276/diamonds
我在这里做错了什么?