文档和其他指南向我建议 Swift 中的 UIScreen.main.bounds 返回屏幕边缘。然而,在 Swift 中使用模拟器并不像这样。
下面是来自XCode模拟器(5s,6)的两个屏幕截图,使用相同的代码。
苹果手机5s苹果手机6
代码在下面,但基本上只是用(屏幕宽度,屏幕高度),(-屏幕宽度,屏幕高度),(屏幕宽度,-屏幕高度),(-屏幕宽度,-屏幕高度)绘制四条路径。
我是否误解了UIScreen.main.bounds?还是有其他原因造成的?理想情况下,我希望能够以编程方式设置找到屏幕四个角的变量。
这一切都是在GameScene中完成的。我在故事板上没有添加任何功能。
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
let pathBound = UIBezierPath(rect: CGRect(x: -screenWidth, y: -screenHeight, width: 5, height: screenHeight*2))
let shapeNode = SKShapeNode(path: pathBound.cgPath)
shapeNode.fillColor = UIColor.green
shapeNode.strokeColor = UIColor.green
addChild(shapeNode)
let pathBound2 = UIBezierPath(rect: CGRect(x: screenWidth-5, y: -screenHeight, width: 5, height: screenHeight*2))
let shapeNode2 = SKShapeNode(path: pathBound2.cgPath)
shapeNode2.fillColor = UIColor.blue
shapeNode2.strokeColor = UIColor.blue
addChild(shapeNode2)
let pathBound3 = UIBezierPath(rect: CGRect(x: -screenWidth, y: screenHeight-5, width: screenWidth*2, height: 5))
let shapeNode3 = SKShapeNode(path: pathBound3.cgPath)
shapeNode3.fillColor = UIColor.red
shapeNode3.strokeColor = UIColor.red
addChild(shapeNode3)
let pathBound4 = UIBezierPath(rect: CGRect(x: -screenWidth, y: -screenHeight, width: screenWidth*2, height: 5))
let shapeNode4 = SKShapeNode(path: pathBound4.cgPath)
shapeNode4.fillColor = UIColor.orange
shapeNode4.strokeColor = UIColor.orange
addChild(shapeNode4)
let pathBound5 = UIBezierPath(rect: CGRect(x: -screenWidth, y: 0
, width: screenWidth*2, height: 5))
let shapeNode5 = SKShapeNode(path: pathBound5.cgPath)
shapeNode5.fillColor = UIColor.purple
shapeNode5.strokeColor = UIColor.purple
addChild(shapeNode5)
你对此有何看法-
var screenWidth = UIScreen.main.bounds.size.width
var screenHeight = UIScreen.main.bounds.size.height