iosswiftxcodeparticlesemitter

Cannot Click UIButton When Particle Emitter Is On Screen


When using a particle emitter, regardless of the position I cannot click the UIButton. If I disable the particle emitter function in viewdidload, I am able to press the UIButton (playButton).

I have tried both zPosition and bringsubviewtofront. I am all out of ideas. Any thoughts?

import Foundation
import UIKit
import SpriteKit

class HomeMenu: UIViewController {

@IBOutlet weak var playButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    
    //multiple emitters = multiple functions!
    //just need to alter positions
    
    //        func bringSubviewToFront(_ view: self.playButton)
    
    particleEmitter()
    
    
    bringButtonToFront()
    
    
}

func bringButtonToFront() {
    self.playButton.bringSubviewToFront(self.playButton)
   
}

func particleEmitter() {
    let sk: SKView = SKView()
    sk.frame = view.bounds
    sk.backgroundColor = .clear
    view.addSubview(sk)

    let scene: SKScene = SKScene(size: view.bounds.size)
    scene.scaleMode = .aspectFit
    scene.backgroundColor = .clear

    let en = SKEmitterNode(fileNamed: "SparkParticle.sks")
    //position of emitter on 2d plane
    en?.position = CGPoint(x:50, y:50)

    scene.addChild(en!)
    sk.presentScene(scene)
    //zposition brought emitter to front of view to make visible
    scene.zPosition = 10
}

@IBAction func playButton(_ sender: Any) {
    performSegue(withIdentifier: "startGaneSegue", sender: self)
}


}

In the image, you can see the emitter is located at the bottom left of the screen, but the button is unclickable. Only when removing the emitter is the button clickable.


Solution

  • self.playButton.bringSubviewToFront(self.playButton) brings the button to the front of itself (doesn't make sense)! Do this instead:

    func bringButtonToFront() {
        self.view.bringSubviewToFront(self.playButton)
    }