我有CAEmitterLayer
,其数组为CAEmitterCell
。我希望每个单元格的输出都混合在一起。现在,数组中的第一个单元格总是位于其他单元格之上。
我调整了图层的renderMode
和每个单元格的zAcceleration
,但都没有解决这个问题。
一些示例代码:
func layoutSubviews() {
// ...
emitterLayer.frame = self.bounds
emitterLayer.seed = UInt32(Date().timeIntervalSinceNow)
emitterLayer.renderMode = kCAEmitterLayerOldestFirst
emitterLayer.emitterPosition = CGPoint(x: bounds.midX, y: bounds.midY)
emitterLayer.emitterSize = CGSize.init(width: bounds.width, height: 0)
emitterLayer.emitterShape = kCAEmitterLayerLine
self.layer.addSublayer(emitterLayer)
}
private func animate() {
// ...
if emitterLayer.emitterCells == nil || emitterLayer.emitterCells?.count == 0 {
let cells = (1...8).map { i -> CAEmitterCell in
if let img = UIImage.init(named: "Slice-\(i)") {
return createEmitterCell(image: img, index: i)
} else {
fatalError()
}
}
emitterLayer.emitterCells = cells
}
}
private func createEmitterCell(image: UIImage, index: Int) -> CAEmitterCell {
let cell = CAEmitterCell.init()
cell.isEnabled = true
cell.contents = image.cgImage
cell.contentsRect = CGRect(origin: CGPoint.zero, size: CGSize(width: 1, height: 1))
cell.birthRate = Float(index)
cell.lifetime = 3
cell.velocity = 7
cell.velocityRange = 3
cell.scale = 0.5
cell.emissionRange = (95 * CGFloat.pi / 180.0)
cell.emissionLatitude = (27 * CGFloat.pi / 180.0)
cell.emissionLongitude = (139 * CGFloat.pi / 180.0)
cell.xAcceleration = 10.0
cell.yAcceleration = 100.0
cell.zAcceleration = 10 * CGFloat(index)
return cell
}
答案 0 :(得分:0)
我有12个发射器单元,我的解决方案是随机化发射器的顺序。这样,应用程序的每次运行都会产生不同的顺序。
emojiCells = "✨✅"
.characters
.sorted(by: { (a, b) -> Bool in
return arc4random() % 2 == 0
}).enumerated().map({ (offset: Int, element: Character) -> CAEmitterCell in
// create emitter cells for each emoji
let raster = rasterize(emoji: element)
return createEmitterCell(image: raster, index: offset)
})
emitterLayer.emitterCells = emojiCells
如果我有更少的发射器单元,那么我将包括彼此重复的发射器。例如[cell1, cell2, cell1]
。其他调整将有助于这种情况,例如提高cell2
的出生率。