我的目标是使用 a 创建一个火焰效果CAEmitterLayer
,其强度可以通过更改alphaSpeed
其给定属性的值来改变CAEmitterCells
。较小的值alphaSpeed
会导致“咆哮”的火灾,而较大的值会抑制火灾。
到目前为止,我有一个CAEmitterLayer
调用的子类,FireEmitterLayer
其初始化程序由以下给出:
convenience init(view: UIView) {
self.init()
emitterPosition = CGPoint(x: 0.5 * view.bounds.width, y: view.bounds.height)
emitterSize = CGSize(width: view.bounds.width, height: 0.05 * view.bounds.height)
renderMode = .additive
emitterShape = .line
emitterCells = fireEmitterCells
}
UIImage
为代表 30x30 火焰图像的 s数组生成发射器单元:
private var fireEmitterCells: FireEmitterCells {
var emitterCells = FireEmitterCells()
for assetIdentifier in assetIdentifiers {
let emitterCell = fireEmitterCell(for: assetIdentifier)
emitterCells.append(emitterCell)
}
return emitterCells
}
每个单元格都是使用此方法创建的:
private func fireEmitterCell(for assetIdentifier: UIImage.AssetIdentifier) -> CAEmitterCell {
let fireEmitterCell = CAEmitterCell()
fireEmitterCell.contents = UIImage(assetIdentifier: assetIdentifier).resized(to: CGSize(width: 20.0, height: 20.0)).cgImage
fireEmitterCell.alphaSpeed = -0.3
fireEmitterCell.birthRate = fireBirthRate
fireEmitterCell.lifetime = fireLifetime
fireEmitterCell.lifetimeRange = 0.5
fireEmitterCell.color = UIColor.init(red: 0.8, green: 0.4, blue: 0.2, alpha: 0.6).cgColor
fireEmitterCell.emissionLongitude = .pi
fireEmitterCell.velocity = 80.0
fireEmitterCell.velocityRange = 5.0
fireEmitterCell.emissionRange = 0.5
fireEmitterCell.yAcceleration = -200.0
fireEmitterCell.scaleSpeed = 0.3
return fireEmitterCell
}
有没有办法alphaSpeed
从这个子类的一个实例中改变这些单元格的值,比如说,fireEmitterLayer
在 some UIViewController
:
var fireEmitterLayer = FireEmitterLayer(view: view)
我试过在FireEmitterLayer
类中添加这个方法
private func setAlphaSpeed(_ alphaSpeed: Float) {
guard let emitterCells = emitterCells else { return }
for emitterCell in emitterCells {
emitterCell.alphaSpeed = alphaSpeed
}
}
但这不起作用...
任何帮助表示赞赏:-)