I\'m building a simple game with Sprite Kit, the screen doesn\'t rotate but I want to know the angle the user is holding the phone for a game mechanic.
The values I
You can get the angle by reading the gyro data provided by CoreMotion. Initialize a CM object like so:
class GameScene: SKScene {
var motionManager = CMMotionManager()
override func didMoveToView(view: SKView) {
motionManager.startGyroUpdates()
}
}
Then in the update function, you can read it:
override func update(currentTime: CFTimeInterval) {
if let data = motionManager.gyroData {
let x = data.rotationRate.x
let y = data.rotationRate.y
let z = data.rotationRate.z
}
}
You can now use the x, y and z values for your game mechanic. However, note that it's giving the current rotation rate and not the absolute rotation. You could keep track of it yourself if that's what you need. Alternatively you could use the accelerometer data:
motionManager.startAccelerometerUpdates()
...
if let data = motionManager.accelerometerData {
let x = data.acceleration.x
}