Why do iOS SpriteKit scene / view coordinates differ between iOS 7.1 and iOS 8?
-
21-12-2019 - |
Question
I am exploring the iOS 8 beta version of SpriteKit, with my project's deployment target set to iOS 7 so I can test on my iPad Air running the released iOS 7.1.
I have found that the coordinates between my SKScene
and its SKView
are not consistent between the simulator (running iOS 8) and my device (running iOS 7.1). Here is the code I've used to isolate the inconsistency:
Starting with the iOS SpriteKit game template, I've modified the settings so that only Landscape Right orientation is supported and I've added the following code to the GameScene
class:
class GameScene: SKScene {
…
var scaleFromView: CGPoint!
override func didMoveToView(view: SKView) {
/* Setup your scene here */
label = SKLabelNode(fontNamed:"Chalkduster")
label.text = "Hello, World!";
label.fontSize = 65;
label.position = CGPoint(x:0.0, y:0.0);
label.zPosition = -1.0
let referencePt = CGPoint(x: 100, y: 100)
var viewPt = convertPointToView(CGPoint(x: 0, y: 0))
viewPt = viewPt + referencePt
let scenePt = convertPointFromView(viewPt)
scaleFromView = scenePt / referencePt
println(" view.bounds is \(view.bounds)")
println(" scene.size is \(scene.size)")
println("scaleFromView is \(scaleFromView)")
world.addChild(label)
}
…
Note this uses some operators I've defined for working with CGPoint
:
@infix func + (a: CGPoint, b: CGPoint) -> CGPoint {
return CGPoint(x: a.x + b.x, y: a.y + b.y)
}
@infix func - (a: CGPoint, b: CGPoint) -> CGPoint {
return CGPoint(x: a.x - b.x, y: a.y - b.y)
}
@infix func * (a: CGPoint, b: CGPoint) -> CGPoint {
return CGPoint(x: a.x * b.x, y: a.y * b.y)
}
@infix func / (a: CGPoint, b: CGPoint) -> CGPoint {
return CGPoint(x: a.x / b.x, y: a.y / b.y)
}
When run under any of the iOS 8 simulators, including the iPad Air simulation, the println
statement logs (as I would expect given the scene and view bounds settings):
view.bounds is (0.0,0.0,1024.0,768.0)
scene.size is (1024.0,768.0)
scaleFromView is (1.0,-1.0)
But when I run it on my device, I get:
view.bounds is (0.0,0.0,768.0,1024.0)
scene.size is (1024.0,768.0)
scaleFromView is (0.75,-0.75)
Thus it seems the view does not properly reflect that it's in landscape orientation. Why is this happening?
PS: I've downloaded the iOS 7.1 simulators and can now report that they behave consistently with the device. That is, this seems to be a bug / feature of iOS 7.1.
Solution
After downloading and installing the iOS 7.1 simulators, I was able to determine that they behave consistently with the device. That is, they too report view.bounds
reflecting portrait rather than landscape mode, even though (or perhaps because) my app is landscape-only.
This seems to be a bug / feature of iOS 7.1.