![](/img/trans.png)
[英]How can I make animation with CAEmitterLayer on SwiftUI?
[英]Adding different objects to CAEmitterLayer with forEach with SwiftUI
當我添加多個 object 時,我必須如下一一寫。 當我想添加 2 個或更多對象時,我總是必須復制和粘貼。 我不希望這樣,我嘗試用 forLoop 來做,但我只看到最后添加的 object。
循環
我寫了一個 function。 ForLoop 的工作量與數組中的圖像數量一樣多。
func prepareParticeCell(particles: [String]) -> CAEmitterCell {
let particleCell = CAEmitterCell()
for particleItem in particles {
let particle = UIImage(named: particleItem)?.cgImage
particleCell.contents = particle
particleCell.name = "Square"
particleCell.birthRate = 5
particleCell.lifetime = 74.5
particleCell.velocityRange = 0.0
particleCell.velocity = 79.0
particleCell.xAcceleration = 0.0
particleCell.yAcceleration = 0.0
particleCell.emissionLatitude = 1*6.0 * (.pi / 180)
particleCell.emissionLongitude = -105.0 * (.pi / 180)
particleCell.emissionRange = 360.0 * (.pi / 180.0)
particleCell.spin = -65.6 * (.pi / 180.0)
particleCell.spinRange = 314.2 * (.pi / 180.0)
particleCell.scale = 0.043
particleCell.scaleRange = 0.7
particleCell.scaleSpeed = 0.02
particleCell.alphaRange = 0.0
particleCell.alphaSpeed = 0.47
particleCell.color = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
}
return particleCell
}
使用 Function
let host = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
let particlesLayer = CAEmitterLayer()
particlesLayer.frame = CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
host.layer.insertSublayer(particlesLayer, at: 0)
host.layer.masksToBounds = true
host.insetsLayoutMarginsFromSafeArea = false
particlesLayer.backgroundColor = .none
particlesLayer.emitterShape = .circle
particlesLayer.emitterPosition = CGPoint(x: 509.4, y: 707.7)
particlesLayer.emitterSize = CGSize(width: 1648.0, height: 1112.0)
particlesLayer.emitterMode = .outline
particlesLayer.renderMode = .backToFront
particlesLayer.emitterCells = [prepareParticeCell(particles: particleImages)] // here
return host
您必須返回一個數組,我為您提供for
解決此問題的兩種方法,一種與另一種與map
。 兩者都在做同樣的事情。
func prepareParticeCellV1(particles: [String]) -> [CAEmitterCell] {
var arrayParticleCell: [CAEmitterCell] = [CAEmitterCell]() // <<: Here
for particleItem in particles {
let particleCell: CAEmitterCell = CAEmitterCell()
particleCell.contents = UIImage(named: particleItem)?.cgImage
particleCell.name = "Square"
particleCell.birthRate = 5
particleCell.lifetime = 74.5
particleCell.velocityRange = 0.0
particleCell.velocity = 79.0
particleCell.xAcceleration = 0.0
particleCell.yAcceleration = 0.0
particleCell.emissionLatitude = 1*6.0 * (.pi / 180)
particleCell.emissionLongitude = -105.0 * (.pi / 180)
particleCell.emissionRange = 360.0 * (.pi / 180.0)
particleCell.spin = -65.6 * (.pi / 180.0)
particleCell.spinRange = 314.2 * (.pi / 180.0)
particleCell.scale = 0.043
particleCell.scaleRange = 0.7
particleCell.scaleSpeed = 0.02
particleCell.alphaRange = 0.0
particleCell.alphaSpeed = 0.47
particleCell.color = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
arrayParticleCell.append(particleCell)
}
return arrayParticleCell
}
func prepareParticeCellV2(particles: [String]) -> [CAEmitterCell] {
return particles.map({ item in
let particleCell: CAEmitterCell = CAEmitterCell()
particleCell.contents = UIImage(named: item)?.cgImage
particleCell.name = "Square"
particleCell.birthRate = 5
particleCell.lifetime = 74.5
particleCell.velocityRange = 0.0
particleCell.velocity = 79.0
particleCell.xAcceleration = 0.0
particleCell.yAcceleration = 0.0
particleCell.emissionLatitude = 1*6.0 * (.pi / 180)
particleCell.emissionLongitude = -105.0 * (.pi / 180)
particleCell.emissionRange = 360.0 * (.pi / 180.0)
particleCell.spin = -65.6 * (.pi / 180.0)
particleCell.spinRange = 314.2 * (.pi / 180.0)
particleCell.scale = 0.043
particleCell.scaleRange = 0.7
particleCell.scaleSpeed = 0.02
particleCell.alphaRange = 0.0
particleCell.alphaSpeed = 0.47
particleCell.color = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0).cgColor
return particleCell
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.