[ad_1]
My app is dependent on how the iPhone is held. For many reasons, I need to use the device sensors with CoreMotion
and the .attitude
property. Reading the device’s attitude is only used for a few times/seconds when the user will set the configuration for my app.
I have tried both pulling and pushing the motion data. In both cases, I can’t figure out how to store the CMAttitude
values in a way that lets me access them elsewhere.
In the code belowβ¦
- The
print
statements inside themanager.startDeviceMotionUpdates
closure work fine. - But β¦ I have ZERO access to the
.attitude
values outside the closure. - The Xcode preview crashes, as well as the app running on my device, if I try to include a
TextView
with the attitude data.
Here is my codeβ¦
import SwiftUI
import CoreMotion
let manager = CMMotionManager()
let queue = OperationQueue()
struct ContentView: View {
@State var myAttitude: CMAttitude = CMAttitude()
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
Button(action: {
guard manager.isDeviceMotionAvailable else { return }
manager.startDeviceMotionUpdates(to: queue) { (motion, error) in
guard let motion else {
return
}
let currentAttitude = motion.attitude
myAttitude = currentAttitude
print("π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’π’")
print("Attitude-Pitch: \(myAttitude)")// βββ These Work Fine!
print("Attitude-Pitch: \(myAttitude.pitch.formatted(.number.precision(.fractionLength(5))))")
print("Attitude-Yaw: \(myAttitude.yaw.formatted(.number.precision(.fractionLength(5))))")
print("Attitude-Roll: \(myAttitude.roll.formatted(.number.precision(.fractionLength(5))))")
print("π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄π΄")
}
}, label: {
Text("Check Your Attitude")
.foregroundStyle(.cyan)
})
Text("Attitude-Pitch: \(myAttitude)") // βββ App Crashes Here!
// Text("Attitude-Pitch: \(myAttitude.pitch.formatted(.number.precision(.fractionLength(5))))")
// Text("Attitude-Yaw: \(myAttitude.yaw.formatted(.number.precision(.fractionLength(5))))")
// Text("Attitude-Roll: \(myAttitude.roll.formatted(.number.precision(.fractionLength(5))))")
}
.padding()
}
}
#Preview {
ContentView(myAttitude: CMAttitude())
}
[ad_2]