0

I have a particle system created with a CAEmitterLayer. The particle effect creates a confetti effect where confetti drops down from the top of the view. The CAEmitterLayer is added as a sublayer to an UIView (specifically created for that purpose in a storyboard, the confettiView) in viewDidLoad() for the view controller:

override func viewDidLoad() {
        ...
        createParticles(for: confettiView)
        ...
}

func createParticles(for view: UIView) {
        let particleEmitter = CAEmitterLayer()
        particleEmitter.emitterPosition = CGPoint(x: view.center.x, y: -96)
        particleEmitter.emitterShape = kCAEmitterLayerLine
        particleEmitter.emitterSize = CGSize(width: view.frame.size.width, height: 1)
        ...
        (adding the emitter cells here)
        ...
        view.layer.addSublayer(particleEmitter)
}

When the UIViewController including the UIView is displayed with the following custom code (by adding it to another UIViewController) the effect is displayed correctly.

parentViewController.addChildViewController(childViewController)
childViewController.frame = parentViewController.view.frame
parentViewController.view.addSubview(childViewController.view)
childViewController.didMove(toParentViewController: parentViewController)

But when I change the view to be presented modally (with UIViewController.present), the effect is not visible:

parentViewController.present(childViewController, animated: true)

Any insights on how to make the particle effect visible when the view controller is presented modally?

jaine
  • 3
  • 1
  • 6
  • Try to play around with different values for modalPresentationStyle property. Also check if this effect works when added in viewDidAppear. – Andrew Jul 02 '19 at 16:52
  • Apparently both the creation of the layer and adding it as a sublayer had to be done in `viewDidAppear` to get this working. Thanks for the tip! – jaine Jul 03 '19 at 08:07
  • At the moment of calling viewDidAppear - controller’s view is added to view hierarchy. So this emitter layer is working only when it is added to active view ( view that have window). – Andrew Jul 03 '19 at 08:35

0 Answers0