File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -10,9 +10,10 @@ import Foundation
1010
1111extension CMAccelerometerData {
1212 /**
13- Get the current device orientation from the given acceleration/gyro data.
13+ Get the current device orientation from the given acceleration/gyro data, or `nil` if it is unknown.
14+ The orientation can only be unknown if the phone is flat, in which case it is not clear which orientation the phone is held in.
1415 */
15- var deviceOrientation : Orientation {
16+ var deviceOrientation : Orientation ? {
1617 let acceleration = acceleration
1718 let xNorm = abs ( acceleration. x)
1819 let yNorm = abs ( acceleration. y)
Original file line number Diff line number Diff line change @@ -161,12 +161,17 @@ final class OrientationManager {
161161 stopDeviceOrientationListener ( )
162162 if motionManager. isAccelerometerAvailable {
163163 motionManager. accelerometerUpdateInterval = 0.2
164- motionManager. startAccelerometerUpdates ( to: operationQueue) { accelerometerData, error in
164+ motionManager. startAccelerometerUpdates ( to: operationQueue) { [ weak self] accelerometerData, error in
165+ guard let self else { return }
165166 if let error {
166167 VisionLogger . log ( level: . error, message: " Failed to get Accelerometer data! \( error) " )
167168 }
168169 if let accelerometerData {
169- self . deviceOrientation = accelerometerData. deviceOrientation
170+ // There is accelerometer data available
171+ if let deviceOrientation = accelerometerData. deviceOrientation {
172+ // The orientation can actually be determined - it is not `nil` (flat)! Update it
173+ self . deviceOrientation = deviceOrientation
174+ }
170175 }
171176 }
172177 }
You can’t perform that action at this time.
0 commit comments