iosswift2caemitterlayer

Keep CAEmitter centered when rotating device


Is there a way to keep CAEmitter centered to UIView (using Autolayout) when rotating device?

I am adding my emitter to viewDidLoad like this:

   // setup emitter
        let emitter = CAEmitterLayer()
        emitter.frame = self.view.bounds

        emitter.renderMode = kCAEmitterLayerAdditive
        emitter.emitterPosition = self.view.center
        self.view.layer.insertSublayer(emitter, atIndex: 1)

        // setup cells
        let cell = CAEmitterCell()
        cell.contents = UIImage(named: "particle")?.CGImage
        cell.birthRate = 1500
        cell.lifetime = 5.0
        cell.color = UIColor(red: 1.0, green: 0.5, blue: 0.1, alpha: 1).CGColor
        cell.alphaSpeed = -0.4
        cell.velocity = 50
        cell.velocityRange = 250

        cell.emissionRange = CGFloat(M_PI) * 2.0

        //add cells to the emitter
        emitter.emitterCells = [cell]

I have tried to remove it and re-add it again but it is not centered....


Solution

  • ok got it. For anyone coming across this...

     override func viewDidLayoutSubviews() {
    
          emitter.emitterPosition = self.view.center
    
         }