我想让我的游戏全屏显示,而不是在两侧和底部都带有黑条。因此我为此找到了这段代码,它适用于场景,现在可以全屏显示,但是资产放置不正确。因为视图的坐标是不同的(不知道为什么,也许因为以前我尝试为safeAreaLayoutGuide设置所有坐标,但后来却出现黑条)。因此,有什么办法可以将其设置回0.5 0.5。否则,我将不得不将场景的anchorPoint设置为视图的anchorPoint,并根据此设置sks文件中的所有资产。

这是GameViewController中的代码:

class GameViewController: UIViewController {

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()

        if let view = self.view as! SKView? {
            view.ignoresSiblingOrder = true
            view.showsFPS = true
            view.showsNodeCount = true

            let scene = HomeScene(size: view.bounds.size)
            scene.scaleMode = .aspectFill
            view.presentScene(scene)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        print("---")
        print("∙ \(type(of: self))")
        print("---")
    }
}


此代码来自HomeScene

let rectangle1 = SKShapeNode(rect: (self.view?.bounds)!)
rectangle1.strokeColor = .white
rectangle1.lineWidth = 20
addChild(rectangle1)

// Set (0, 0) as the centre of the screen
scene?.anchorPoint = CGPoint(x: 0.5, y: 0.5)

let rectangle = SKShapeNode(rect: self.frame)
rectangle.strokeColor = .green
rectangle.lineWidth = 20
addChild(rectangle)


这就是场景在0.5、0.5处的外观,但是视图上方是绿色的场景,白色是视图,视图的中间是按钮:

ios - 如何将anchorPoint of view设置为0.5,0.5以在屏幕中心获取 View ? (不是场景)-LMLPHP

谢谢。

最佳答案

您可以使用其他锚点来解决问题,也可以将白色“视图”节点在场景中的位置不同。

有关坐标系(和锚点)的信息:apple documentation

根据文档,默认锚位置为(0.5,0.5)。将白色视图置于左下角:减去x和y的宽度和高度的一半。请注意,线宽也会影响定位。将白色视图置于左下方:

let bottomLeft: CGPoint = CGPoint(x: -self.size.width/2 + rectangle1.lineWidth/2,
                                  y: -self.size.height/2 + rectangle1.lineWidth/2)
rectangle1.position = bottomLeft


要将白色视图在绿色视图中居中,请减去矩形1的宽度和高度(再次注意线宽)

let center: CGPoint = CGPoint(x: -rectangle1.frame.size.width/2 + rectangle1.lineWidth/2,
                              y: -rectangle1.frame.size.height/2 + rectangle1.lineWidth/2)
rectangle1.position = center


请注意,您的绿色视图位于白色视图上方。将白色视图的z位置设置为某个正数,以将其排列在绿色视图上方

rectangle1.zPosition = 1

关于ios - 如何将anchorPoint of view设置为0.5,0.5以在屏幕中心获取 View ? (不是场景),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54004414/

10-09 07:35
查看更多