【问题标题】:iOS scene size changing?iOS 场景大小变化?
【发布时间】:2018-04-12 02:23:12
【问题描述】:

我有一个在 SpriteKit 中构建的游戏,我正在尝试为 iPhone X 调整它的大小。

这是我的初始设置代码:

override func viewDidLoad() {
    super.viewDidLoad()
    authenticateLocalPlayer()
    var isiphonex = false
    if UIDevice().userInterfaceIdiom == .phone {
        if UIScreen.main.nativeBounds.height == 2436{
            isiphonex=true
        }
    }

    let skView = self.view as! SKView
    if(isiphonex){
        if #available(iOS 11.0, *) {
            print("ios11")

            skView.frame = CGRect(x: 39 , y: 0, width: 667, height: skView.safeAreaLayoutGuide.layoutFrame.height)

        } else {
            // Fallback on earlier versions
        }



    }

    let startScene = StartScene(size:skView.bounds.size)


    startScene.anchorPoint = CGPoint(x: 0.0892857, y: 0)
    skView.showsFPS = false
    skView.showsNodeCount = false

    skView.ignoresSiblingOrder = true
    startScene.scaleMode = .resizeFill

    skView.presentScene(startScene)
}

然后当场景加载时我这样做:

override func didMove(to view: SKView) {
    print(scene?.size.width)
    print(view.bounds.size.width)
    print(scene!.view!.bounds.size.width)
}

它们都打印 667

但是当我试图在触摸过程中改变场景并这样做时:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print(scene!.size.width)
    print(scene!.view!.bounds.size.width)

}

他们都打印 812。

为什么会这样,我怎样才能让它保持在 667?

【问题讨论】:

    标签: ios xcode uiview sprite-kit skscene


    【解决方案1】:
    1. 尝试在 viewWillAppear 或 viewDidAppear 中设置框架。
    2. 检查是否应用了覆盖您正在设置的框架的任何约束。

    【讨论】:

      【解决方案2】:

      显然改变 ViewController 的主要场景的大小只会导致它恢复到原始大小。为了解决这个问题,我创建了一个新的 SKView 并将其作为子视图添加到 ViewController 的主视图中。然后我在那个新的 SKView 中展示了我所有的 SKSCenes

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 2013-08-21
        • 2019-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-08
        相关资源
        最近更新 更多