2017-08-09 36 views
0

我有一个CAEmitterLayerCAEmitterCell的数组。我希望每个细胞的输出都混合在一起。现在数组中的第一个单元格总是绘制在其他单元格之上。CAEmitterLayer多个单元交错

我已经调整了图层的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

它发生在我身上,排列在阵列中的重复发射器可能会产生这种效果。例如:'[cell1,cell2,cell1]' – JoePasq

回答

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的出生率。