我的目标是使用一个CAEmitterLayer
来创建一个射击效果,可以通过更改给定alphaSpeed
的{{1}}属性的值来更改其强度。较小的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
}
阵列生成的发射器单元代表了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
的实例中更改这些单元格的fireEmitterLayer
值:
UIViewController
我尝试在var fireEmitterLayer = FireEmitterLayer(view: view)
类中添加此方法
FireEmitterLayer
但这不起作用...
感谢您的帮助:-)
答案 0 :(得分:0)
每当我要使用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速度,比例,比例范围,比例速度等设置为最适合轰鸣或压制。在很大程度上取决于您在顶层的灵活性。