[英]iOS (Swift) Changing a property of a CAEmitterCell after it's been initialised
我的目標是使用CAEmitterLayer
創建火焰效果,其強度可以通過更改其給定CAEmitterCells
的alphaSpeed
屬性的值來改變。 較小的alphaSpeed
值會導致“咆哮”的火焰,而較大的值會抑制火焰。
到目前為止,我有一個名為FireEmitterLayer
的CAEmitterLayer
的子類,它的初始化程序如下:
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
}
發射器單元是為代表 30x30 火焰圖像的UIImage
數組生成的:
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
值,例如在某些UIViewController
中稱為fireEmitterLayer
:
var fireEmitterLayer = FireEmitterLayer(view: view)
我嘗試在FireEmitterLayer
類中添加此方法
private func setAlphaSpeed(_ alphaSpeed: Float) {
guard let emitterCells = emitterCells else { return }
for emitterCell in emitterCells {
emitterCell.alphaSpeed = alphaSpeed
}
}
但這不起作用...
任何幫助表示贊賞:-)
每當我想使用 func 時,我都會為我的發射器動態設置我的單元格,如下所示:
func makeEmitterCell(thisEmitter : CAEmitterLayer, newColor: UIColor, priorColor: UIColor, contentImage : UIImage) -> CAEmitterCell {
let cell = CAEmitterCell()
cell.birthRate = lastChosenBirthRate
cell.lifetime = lastChosenLifetime
cell.lifetimeRange = lastChosenLifetimeRange
//cell.color = newColor.cgColor
cell.emissionLongitude = lastChosenEmissionLongitude
cell.emissionLatitude = lastChosenEmissionLatitude
cell.emissionRange = lastChosenEmissionRange
cell.spin = lastChosenSpin
cell.spinRange = lastChosenSpinRange
cell.scale = lastChosenScale
cell.scaleRange = lastChosenScaleRange
cell.scaleSpeed = lastChosenScaleSpeed
cell.alphaSpeed = Float(lastChosenAlphaSpeed)
cell.alphaRange = Float(lastChosenAlphaRange)
cell.autoreverses = lastChosenAutoReverses
cell.isEnabled = lastChosenIsEnabled
cell.velocity = lastChosenVelocity
cell.velocityRange = lastChosenVelocityRange
cell.xAcceleration = lastChosen_X_Acceleration
cell.yAcceleration = lastChosen_Y_Acceleration
cell.zAcceleration = 0
cell.contents = contentImage.cgImage
//Access the property with this key path format: @"emitterCells.<name>.<property>"
cell.name = "myCellName"
let animation = CABasicAnimation(keyPath: "emitterCells.myCellName.color")
animation.fromValue = priorColor.cgColor
animation.toValue = newColor.cgColor
animation.duration = CFTimeInterval(cellAnimationTimeValue)
if thisEmitter == particleEmitter_1
{
particleEmitter_1.add(animation, forKey: "emitterCells.myCellName.color")
}
if thisEmitter == particleEmitter_2
{
particleEmitter_2.add(animation, forKey: "emitterCells.myCellName.color")
}
return cell
} // ends makeEmitterCell
在該功能的末尾,我的設計有一個彩色動畫,您可以通過某種方式獲得獎勵代碼。
我保留這些變量以允許我動態控制單元格功能;
var lastChosenBirthRate : Float = 300
var lastChosenLifetime : Float = 3
var lastChosenLifetimeRange : Float = 0
var lastChosenEmissionLongitude : CGFloat = 2 * CGFloat.pi
var lastChosenEmissionLatitude : CGFloat = 2 * CGFloat.pi
var lastChosenEmissionRange : CGFloat = 2 * CGFloat.pi
var lastChosenSpin : CGFloat = 1
var lastChosenSpinRange : CGFloat = 0
var lastChosenScale : CGFloat = 1
var lastChosenScaleRange : CGFloat = 0
var lastChosenScaleSpeed : CGFloat = -0.3
var lastChosenAlphaSpeed : CGFloat = -0.1
var lastChosenAlphaRange : CGFloat = 0
var lastChosenAutoReverses : Bool = false
var lastChosenIsEnabled : Bool = true
var lastChosenVelocity : CGFloat = 20
var lastChosenVelocityRange : CGFloat = 0
var lastChosen_X_Acceleration : CGFloat = 0
var lastChosen_Y_Acceleration : CGFloat = 0
我在我的代碼中的幾個地方制作了發射器單元。 這是一個。 此函數的一部分制作發射器單元並將它們設置為所需的發射器:
func changeParticlesCellImageRightNow(thisEmitter : CAEmitterLayer, thisContentImage : UIImage, theColor : UIColor)
{
var priorColorToPassDown = UIColor()
if thisEmitter == particleEmitter_1
{
priorColorToPassDown = lastColorUsedEmitter1
}
if thisEmitter == particleEmitter_2
{
priorColorToPassDown = lastColorUsedEmitter2
}
let color1 = makeEmitterCell(thisEmitter: thisEmitter, newColor: theColor, priorColor: priorColorToPassDown, contentImage: thisContentImage)
thisEmitter.emitterCells = [color1]
} // ends changeParticlesCellImageRightNow
這是對該函數的調用:
// Force the change to occur right now, using the existing colors
changeParticlesCellImageRightNow(thisEmitter : particleEmitter_1, thisContentImage : emitter_1_Image, theColor : lastColorUsedInGeneral)
changeParticlesCellImageRightNow(thisEmitter : particleEmitter_2, thisContentImage : emitter_2_Image, theColor : lastColorUsedInGeneral)
這是發射器層對象
let particleEmitter_1 = CAEmitterLayer()
let particleEmitter_2 = CAEmitterLayer()
您可能能夠以類似的方式拼湊出一種方法,將發射器細胞的出生率、速度、速度范圍、alpha 速度、比例、比例范圍、比例速度等設置為最適合咆哮或抑制。 很大程度上取決於您在頂層的靈活性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.