0

I am working on SpriteKit Game. From within GameScene i want to represent another viewController. I have done it correctly, but when i dismiss Modally presented viewController, GameScene on my Root viewController freezes. Here is my code on GameScene....

     NotificationCenter.default.post(name: NSNotification.Name(rawValue:"present"), object:nil)

Here is my code for root viewController(GameViewController)

override func viewDidLoad() {
    super.viewDidLoad()

    let gameScene = GameScene()
    let skView = self.view as! SKView
    skView.showsFPS = true
    skView.ignoresSiblingOrder = true
    let size = CGSize(width:590, height:390)
    /* Set the scale mode to scale to fit the window */
    //menuScene.scaleMode = .aspectFit
    // menuScene.anchorPoint = CGPoint(x:0, y:0)
    //skView.allowsTransparency = true
    //size our scene to fit the view exactly:
    //    let rect1 = CGRect(origin:point, size:size)

    //skView.setNeedsUpdateConstraints()
    gameScene.size = CGSize(width:size.width, height:size.height)
    gameScene.scaleMode = .aspectFill
    skView.presentScene(gameScene)
    skView.translatesAutoresizingMaskIntoConstraints = false


    skView.ignoresSiblingOrder = true

    skView.showsFPS = true
    skView.showsNodeCount = true
    NotificationCenter.default.addObserver(self, selector: #selector(self.presentVC), name: NSNotification.Name(rawValue:"present"), object: nil)
}

override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden: Bool {
    return true
}
func presentVC(){
    let toViewController = viewController() as UIViewController
    toViewController.modalPresentationStyle = .overFullScreen
    self.present(toViewController, animated: true, completion: nil)
    toViewController.transitioningDelegate = self

    print("presenting next")
}

And Here is the code for another viewController that is to be represented modally over rootViewController:

override func viewDidLoad() {
    super.viewDidLoad()
    let imageView = UIImageView(image:UIImage(named:"Background1"))
    imageView.frame = CGRect(x:0, y:0, width:1000, height:800)
    let aButton = UIButton()
    aButton.frame = CGRect(x:view.frame.width/2 - 50, y:view.frame.height/2 - 50, width:100, height:100)
    aButton.setTitle("aButton", for: .normal)
    aButton.backgroundColor = .green
    aButton.addTarget(self, action: #selector(pressed), for: .touchUpInside)
    aButton.isEnabled = true

    view.addSubview(imageView)
   view.addSubview(aButton)
}
func pressed(){
    dismissVC()}

override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden: Bool {
    return true
}
func dismissVC(){
  self.dismiss(animated: true, completion: nil)
        print("returning previous")
}
Jay Patel
  • 2,642
  • 2
  • 18
  • 40
Apar Parmar
  • 79
  • 1
  • 6
  • I found solution for this problem. – Apar Parmar Jan 17 '18 at 10:36
  • if your not gonna post an answer for your question after you've discovered what was wrong then you should delete the question. An open question that you know the answer to but with no answer posted does no one any good – Ron Myschuk Jan 17 '18 at 14:31
  • Dear, Ron Myschuk... I posted this question one day before and i found the solution on same day.... In hurry i forgot to post whole answer here. I am posting answer for this question in a moment..... – Apar Parmar Jan 18 '18 at 12:42

1 Answers1

1

Here is the answer with explanation. First of all it was not GameScene in frozen state. But it was view of another viewController that was present even after completion of activity on modally presented viewController. Presence of view of another viewController was due to nonExecution of viewController dismiss code inside that viewController. So presence of another viewController view was not allowing me to interact with my app. Now Solution is, In above Posted question, In GameViewController code i have added presentVC() function. Inside that instead of line:

    self.present(toViewController, animated: true, completion: nil)

Use following line:

    self.show(toViewController, sender:nil)
Apar Parmar
  • 79
  • 1
  • 6