iOS 设备旋转时的视图和场景不正确

view and scene not correct on iOS device rotation

旋转时,我的位置不对view/scene。 viewWillTransition 方法如下:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
{
  let newRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  let newSize = CGSize(width: size.width, height: size.height)

  self.view.frame = newRect

  let skView = self.view as! SKView      
  if let sceneNode = skView.scene as! GameScene? {      
    sceneNode.size = newSize

    sceneNode.position = CGPoint(x: self.view.frame.midX, y: self.view.frame.midY)

  }
}

有人可以告诉我我做错了什么吗?

谢谢

试试这个,

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let skView = self.view as! SKView
        if let sceneNode = skView.scene as! GameScene? {
            sceneNode.size = self.view.frame.size

            sceneNode.position = CGPoint(x: self.view.frame.midX, y: self.view.frame.midY)

        }
    }

谢谢@AshokPolu!
我更喜欢这个,因为尺寸会随着设备旋转动画而变化。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animateAlongsideTransition(in: self.view, animation: { (_) in
        if let skView = self.view as? SKView, let scene = skView.scene as? GameScene {
            scene.size = self.view.frame.size
            scene.position = CGPoint(x: self.view.frame.midX, y: self.view.frame.midY)
        }
    }, completion: nil)
}