A very bizarre issue we've been seeing (gifs below),
TeamBadgeView
,
which is a button that emits emoji as CAEmitterCells
CAEmitterCell
becomes more and more unresponsiveCAEmitterLayer
and CAEmitterCell
around, holding a reference in the button, and declaring locally, but similar issuesaction
is being fired correct, everytime I spam the button. It's just that the emitter cell is not rendering for a few seconds. And some of the emitter cells just don't render at allIt's gotten to the mind-boggling point, does anybody have any ideas or leads on what this could be?
First presentation of ViewController:
After 5th presentation of ViewController (Pressing button at same rate):
ViewController code:
let teamBadgeView = TeamBadgeView.fromNib()
teamBadgeView.configure()
Button code:
class TeamBadgeView: UIView {
let emitter = CAEmitterLayer()
let fireSize = CGSize(width: 16, height: 18)
let fireScale: CGFloat = 0.8
func configure() {
emitter.seed = UInt32(CACurrentMediaTime())
emitter.emitterPosition = CGPoint(x: bounds.midX, y: 0)
emitter.emitterShape = CAEmitterLayerEmitterShape.line
emitter.emitterSize = fireSize
emitter.renderMode = CAEmitterLayerRenderMode.additive
layer.addSublayer(emitter)
}
@IBAction func tapAction(_ sender: Any) {
emitFire()
}
private func emitFire() {
let cell = CAEmitterCell()
let beginTime = CACurrentMediaTime()
cell.birthRate = 1
cell.beginTime = beginTime
cell.duration = 1
cell.lifetime = 1
cell.velocity = 250
cell.velocityRange = 50
cell.yAcceleration = 100
cell.alphaSpeed = -1.5
cell.scale = fireScale
cell.emissionRange = .pi/8
cell.contents = NSAttributedString(string: "🔥").toImage(size: fireSize)?.cgImage
emitter.emitterCells = [cell]
}
}
Instead of setting the emitterCells
array every time:
emitter.emitterCells = [cell]
...append the new cell to it. Make sure to initialize it to an empty array if it's nil though, or else the append will not work:
if emitter.emitterCells == nil {
emitter.emitterCells = []
}
emitter.emitterCells?.append(cell)