File tree Expand file tree Collapse file tree 2 files changed +10
-4
lines changed Expand file tree Collapse file tree 2 files changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -10,9 +10,10 @@ import Foundation
10
10
11
11
extension CMAccelerometerData {
12
12
/**
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.
14
15
*/
15
- var deviceOrientation : Orientation {
16
+ var deviceOrientation : Orientation ? {
16
17
let acceleration = acceleration
17
18
let xNorm = abs ( acceleration. x)
18
19
let yNorm = abs ( acceleration. y)
Original file line number Diff line number Diff line change @@ -161,12 +161,17 @@ final class OrientationManager {
161
161
stopDeviceOrientationListener ( )
162
162
if motionManager. isAccelerometerAvailable {
163
163
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 }
165
166
if let error {
166
167
VisionLogger . log ( level: . error, message: " Failed to get Accelerometer data! \( error) " )
167
168
}
168
169
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
+ }
170
175
}
171
176
}
172
177
}
You can’t perform that action at this time.
0 commit comments