简体   繁体   English

SpriteKit场景不适用于iPhone X的整个视图

[英]SpriteKit Scene doesn't fit entire view on the iPhone X

The iPhone X is released a few days ago. iPhone X于几天前发布。 I am trying to update one of my game to the new phone. 我正在尝试将我的游戏之一更新为新手机。 However, on one scene with SpriteKit scene, the scene doesn't scale to fill the entire screen. 但是,在具有SpriteKit场景的一个场景中,该场景无法缩放以填满整个屏幕。

Here is the code to display the scene: 这是显示场景的代码:

GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"];
scene.scaleMode = SKSceneScaleModeFill;
// skView is basically self.view casted to SKView
[skView presentScene:scene];

The size of the scene in the SKS file is 2048x1536. SKS文件中的场景大小为2048x1536。

This is what it looks like: 看起来是这样的:

图片

The red part is the scene content. 红色部分是场景内容。 One interesting thing is, the background (which is the blue and gray, does get displayed properly. 一件有趣的事是,背景(蓝色和灰色确实可以正确显示)。

Anyway to fix this? 有任何解决这个问题的方法吗? I'm fine with just stretching the scene to fill the display (there won't be anything covered up by the rounded corner and the notch) 我只需要拉伸场景以填充显示就可以了(圆角和凹槽不会覆盖任何东西)

There might be a cleaner approach for SpriteKit games to support iPhone X screen, but I came out with the following extension for SKView: SpriteKit游戏可能有更清洁的方法来支持iPhone X屏幕,但我为SKView提供了以下扩展名:

public extension SKView {
    func isIphoneX() -> Bool {
        let screenHeight = 2436.0
        let screenWidth = 1125.0
        let iphoneXAspectRatio = screenHeight / screenWidth

        let aspectRatio = Double(self.frame.width/self.frame.height)
        return (aspectRatio == iphoneXAspectRatio) ? true : false
    }
}

And the usage: Basically it sets the scale mode according to the screen's aspect ratio. 用法:基本上,它根据屏幕的纵横比设置缩放模式。 If it's an iPhone X aspect ratio set the scale mode to "resizeFill", otherwise for the classic rectangle iPhone and iPad screens use the "aspectFill" mode. 如果是iPhone X长宽比,则将缩放模式设置为“ resizeFill”,否则,对于经典的矩形iPhone和iPad屏幕,请使用“ aspectFill”模式。

import SpriteKit

class GameViewController: UIViewController {

    // MARK: - View lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()

        guard let view = self.view as? SKView else { return }
        guard let scene = SKScene(fileNamed: "sceneName") else { return }
        scene.scaleMode = view.isIphoneX() ? .resizeFill : .aspectFill

        view.presentScene(scene)
    }
}

I found that resizeFill mode is the only way to make the content fill the whole iPhone X screen without distorting or clipping off the scene's content. 我发现resizeFill模式是使内容填充整个iPhone X屏幕而不会扭曲或剪切场景内容的唯一方法。 If you have a HUD node in the game (score, counters, buttons, etc...) you need to make changes to that as well! 如果您在游戏中有一个HUD节点(分数,计数器,按钮等),则也需要对其进行更改!

override func didMove(to view: SKView) {
   // Any additional setup... 
   ...

    if view.isIPhoneX {
        hud = SKReferenceNode(fileNamed: "yourIPhoneXHUDScene")
    } else {
        hud = SKReferenceNode(fileNamed: "yourStandardHUDScene")
    }

    cam.addChild(hud)
}

Tested with every iPhone screen size (3.5", 4", 4.7", 5.5", 5.8") in the simulator. However I'm not sure how to deal with gamescenes with retina iPad size (2048 x 1536) set in Xcode. 在模拟器中对每个iPhone屏幕尺寸(3.5“,4”,4.7“,5.5”,5.8“)进行了测试。但是,我不确定如何处理Xcode中设置的视网膜iPad尺寸(2048 x 1536)的游戏场景。

aspectFill works fine for me. AspectFill对我来说效果很好。 The scene is clipped on the left and on the right but it's not distorted. 场景在左侧和右侧被剪切,但没有失真。

I have SKView resized automatically with AutoLayout and then I present my Scene with aspectFill. 我已使用AutoLayout自动调整了SKView的大小,然后使用AspectFill呈现了我的场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM