【问题标题】:My GameScene Freezes when i dismiss modally presented viewController当我关闭模态呈现的 viewController 时,我的 GameScene 冻结
【发布时间】:2018-06-25 23:33:57
【问题描述】:

我正在开发 SpriteKit 游戏。在 GameScene 中,我想代表另一个 viewController。我做得对,但是当我关闭模态呈现的视图控制器时,我的根视图控制器上的 GameScene 冻结。这是我在 GameScene 上的代码......

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

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

【问题讨论】:

  • 我找到了解决这个问题的方法。
  • 如果您在发现问题后不打算为您的问题发布答案,那么您应该删除该问题。一个您知道答案但没有发布答案的开放式问题对任何人都没有好处
  • 亲爱的,Ron Myschuk...我在前一天发布了这个问题,我在同一天找到了解决方案...。匆忙我忘了在这里发布整个答案。我稍后会发布这个问题的答案.....

标签: ios swift uiviewcontroller sprite-kit uimodalpresentationstyle


【解决方案1】:

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

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

使用以下行:

    self.show(toViewController, sender:nil)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多