-
-
Save Natata/f18dc528d0ebdf14b166ca4bc425c52e to your computer and use it in GitHub Desktop.
Determine the direction of "gaze" of the device in any orientation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CMDeviceMotion { | |
func gaze(atOrientation orientation: UIInterfaceOrientation) -> SCNVector4 { | |
let attitude = self.attitude.quaternion | |
let aq = GLKQuaternionMake(Float(attitude.x), Float(attitude.y), Float(attitude.z), Float(attitude.w)) | |
let pi2 = Double.pi/2 | |
let final: SCNVector4 | |
switch UIApplication.shared.statusBarOrientation { | |
case .landscapeRight: | |
let cq = GLKQuaternionMakeWithAngleAndAxis(Float(pi2), 0, 1, 0) | |
let q = GLKQuaternionMultiply(cq, aq) | |
final = SCNVector4(x: -q.y, y: q.x, z: q.z, w: q.w) | |
case .landscapeLeft: | |
let cq = GLKQuaternionMakeWithAngleAndAxis(Float(-pi2), 0, 1, 0) | |
let q = GLKQuaternionMultiply(cq, aq) | |
final = SCNVector4(x: q.y, y: -q.x, z: q.z, w: q.w) | |
case .portraitUpsideDown: | |
let cq = GLKQuaternionMakeWithAngleAndAxis(Float(pi2), 1, 0, 0) | |
let q = GLKQuaternionMultiply(cq, aq) | |
final = SCNVector4(x: -q.x, y: -q.y, z: q.z, w: q.w) | |
case .unknown: | |
fallthrough | |
case .portrait: | |
let cq = GLKQuaternionMakeWithAngleAndAxis(Float(-pi2), 1, 0, 0) | |
let q = GLKQuaternionMultiply(cq, aq) | |
final = SCNVector4(x: q.x, y: q.y, z: q.z, w: q.w) | |
} | |
return final | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated to Swift 3