I have a problem with measuring a rotation with iPhone - while in move, device seems to be "fooled" and doesn't show exact deviation. I'm using the following code to rotate the compass-like wheel:
if manager.isDeviceMotionAvailable {
manager.deviceMotionUpdateInterval = 0.01
manager.startDeviceMotionUpdates(to: queue) {
[weak self] (data: CMDeviceMotion?, error: Error?) in
if let gravity = data?.gravity {
let rotation = atan2(gravity.x, gravity.y) - M_PI //compute rotation
self?.imageView.transform = CGAffineTransform(rotationAngle: rotation)
}
}
}
Everything is fine, while iPhone is not moving. To illustrate I attach a short clip (problem is seen especially at the end of the clip). You can see that the read-out (green trace) is delayed and sometimes shows wrong angle (in comparison to car's steering wheel position). Is that because I'm using gravity variables? Should I use something else like rotationRate for example? Or maybe it's a different problem? Thanks!