2

我的目标是使用 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
    }
}

但这不起作用...

任何帮助表示赞赏:-)

4

1 回答 1

0

每当我想使用以下函数时,我都会为我的发射器动态设置我的单元格:

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

这是对该 func 的调用:

    // 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 速度、比例、比例范围、比例速度等设置为最适合咆哮或抑制。很大程度上取决于您在顶层的灵活性。

于 2020-04-02T21:10:59.807 回答