I'm building an app that needs to take level pictures. I want to use the gyroscope to make sure the phone is level when taking a picture.
I've gotten the gyroscope updates(which gradually increase even though the device isn't moving) but I can't seem to find out how to get the initial gyroscope cordinates.
How can I get the initial values of the gyroscope?
Here's a condensed version of my code
import UIKit
import CoreMotion
class SizingFlowFrontPhotoViewController: UIViewController, AVCapturePhotoCaptureDelegate {
var gyroX:Double = 0.0
var gyroY:Double = 0.0
var gyroZ:Double = 0.0
var motionManager = CMMotionManager()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
motionManager.gyroUpdateInterval = 0.2
motionManager.startGyroUpdates(to: OperationQueue.current!) { (data, error) in
if let myData = data {
// x = rotate it on sim card (front flip/back flip)
// y = rotate it on charger (twister spin)
// z = rotate it on tilt (landscape vs portrait)
self.gyroX += myData.rotationRate.x
self.gyroY += myData.rotationRate.y
self.gyroZ += myData.rotationRate.z
print("gyroX: \(self.gyroX) - gyroY: \(self.gyroY) - gyroZ: \(self.gyroZ)")
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
motionManager.stopGyroUpdates()
}
}