当我解除模态呈现的viewController时,我的GameScene冻结了

时间:2018-01-17 06:48:44

标签: ios swift uiviewcontroller sprite-kit uimodalpresentationstyle

我正在研究SpriteKit Game。从GameScene中我想代表另一个viewController。我已经正确完成了,但是当我解雇Modally提出的viewController时,我的Root viewController上的GameScene冻结了。这是我在GameScene上的代码......

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

这是我的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")
}

以下是另一个viewController的代码,它将在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")
}

1 个答案:

答案 0 :(得分:1)

以下是解释的答案。首先,GameScene并非处于冻结状态。但它是另一个viewController的视图,即使在模态呈现的viewController上完成活动后也存在。存在另一个viewController的视图是由于viewController中的非执行解除了该viewController内部的代码。因此,另一个viewController视图的存在不允许我与我的应用程序进行交互。现在解决方法是,在上面发布的问题,在GameViewController代码中我添加了presentVC()函数。在里面而不是行:

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

使用以下行:

    self.show(toViewController, sender:nil)