微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我可以使用布局约束使GameScene:SKScene出现,但是为什么我仍然可以在安全区域内绘制?

如何解决我可以使用布局约束使GameScene:SKScene出现,但是为什么我仍然可以在安全区域内绘制?

下面是我用来展示GameScene的GameViewController中的代码。 GameScene尊重安全区(iPhone X)。

但是,如果我创建一个SKSprite节点,则上方屏幕的坐标将绘制到安全区域中。

我的印象是不会发生,也找不到顶部和底部的锚点。

p.s。附带说明,我无法在调试器中查看 originalS.frame.width originalS.frame.height ,它们以无效表达式形式出现。但是他们不会使应用程序崩溃。

class GameViewController: UIViewController {

    private let myView : UIView = {
        let myView = UIView()
        myView.translatesAutoresizingMaskIntoConstraints = false
        myView.backgroundColor = .clear
        return myView
    }()
    
    private func addConstraints(){
        var constraints = [NSLayoutConstraint]()
        
        constraints.append(myView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor))
        constraints.append(myView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor))
        constraints.append(myView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor))
        constraints.append(myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor))

        NSLayoutConstraint.activate(constraints)
        
    }
    
    let logo = UIImage(named: "startup")
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(myView)
        addConstraints()
        
        if let view = self.view as! SKView?
        {
            
            myGlobalVars.widthPixels = UIScreen.main.nativeBounds.width
            myGlobalVars.heightPixels = UIScreen.main.nativeBounds.height
            myGlobalVars.widthPoints = UIScreen.main.bounds.width
            myGlobalVars.heightPoints = UIScreen.main.bounds.height

            originalS = myView
            originalS.backgroundColor = .clear

            originalS.translatesAutoresizingMaskIntoConstraints = false
            originalS.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
            originalS.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor,constant: 0).isActive = true
            originalS.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
            originalS.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,constant: -0).isActive = true
            

            var scene : GameScene!
                dispatchQueue.main.async {
                     scene = GameScene(size: CGSize(width: originalS.frame.width,height: originalS.frame.height))

            
            scene.anchorPoint = CGPoint(x: 0.0,y: 0.0)
                scene.backgroundColor = .black
                scene.scaleMode = .aspectFit
                myGlobalVars.sceneRect = scene.frame
                myGlobalVars.gameScene = scene
                view.isHidden = false
                view.presentScene(scene)
            }
            myGlobalVars.passGo = true
            

            view.ignoresSiblingOrder = true
            view.showsFPS = true
            view.showsNodeCount = true
            view.showsPhysics = false


            
        }
    }

解决方法

在另一个董事会上,有人告诉我确保我从子视图 myView

启动GameScene。
import UIKit
import SpriteKit
import GameplayKit

var myGlobalVars = GlobalVars(backGround: SKSpriteNode(),pegHolder: SKSpriteNode(),currentNode: SKSpriteNode(),widthPoints: 0.0,heightPoints: 0.0,widthPixels: 0.0,heightPixels: 0.0,passGo: false,sceneRect: .zero,fileName: " ")

class GameViewController: UIViewController {

    private let myView : UIView = {
        let myView = UIView()
        myView.translatesAutoresizingMaskIntoConstraints = false
        myView.backgroundColor = .clear
        return myView
    }()
    
    private func addConstraints(){
        var constraints = [NSLayoutConstraint]()
        
        constraints.append(myView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor))
        constraints.append(myView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor))
        constraints.append(myView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor))
        constraints.append(myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor))

        
        NSLayoutConstraint.activate(constraints)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let topSafeArea: CGFloat
        let bottomSafeArea: CGFloat

        if #available(iOS 11.0,*) {
            topSafeArea = view.safeAreaInsets.top
            bottomSafeArea = view.safeAreaInsets.bottom
        } else {
            topSafeArea = topLayoutGuide.length
            bottomSafeArea = bottomLayoutGuide.length
        }
    }

    let logo = UIImage(named: "startup")
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(myView)
        addConstraints()
        
        if let view = self.view as! SKView?
        {
            
            myGlobalVars.widthPixels = UIScreen.main.nativeBounds.width
            myGlobalVars.heightPixels = UIScreen.main.nativeBounds.height
            myGlobalVars.widthPoints = UIScreen.main.bounds.width
            myGlobalVars.heightPoints = UIScreen.main.bounds.height

            var scene : GameScene!
            DispatchQueue.main.async { [self] in
                     scene = GameScene(size: CGSize(width: myView.frame.width,height: myView.frame.height))

            scene.anchorPoint = CGPoint(x: 0.0,y: 0.0)
                scene.backgroundColor = .clear
                scene.scaleMode = .aspectFit
                myGlobalVars.sceneRect = scene.frame
                myGlobalVars.gameScene = scene
                view.isHidden = false
                view.presentScene(scene)
            }
            myGlobalVars.passGo = true
            

            view.ignoresSiblingOrder = true
            view.showsFPS = true
            view.showsNodeCount = true
        }
    }
    
    override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return false
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。