Tuesday, December 26, 2023
HomeiOS DevelopmentWhat's easiest strategy to show `CMMotionManager.perspective` in a TextView?

What’s easiest strategy to show `CMMotionManager.perspective` in a TextView?


My app relies on how the iPhone is held. For a lot of causes, I want to make use of the gadget sensors with CoreMotion and the .perspective property. Studying the gadget’s perspective is barely used for just a few occasions/seconds when the consumer will set the configuration for my app.

I’ve tried each pulling and pushing the movement knowledge. In each instances, I can not work out learn how to retailer the CMAttitude values in a approach that lets me entry them elsewhere.

Within the code beneath…

  • The print statements contained in the supervisor.startDeviceMotionUpdates closure work advantageous.
  • However … I’ve ZERO entry to the .perspective values outdoors the closure.
  • The Xcode preview crashes, in addition to the app operating on my gadget, if I attempt to embody a TextView with the perspective knowledge.

Right here is my code…

import SwiftUI
import CoreMotion

let supervisor = CMMotionManager()
let queue = OperationQueue()

struct ContentView: View {
   
   @State var myAttitude: CMAttitude = CMAttitude()
   
   var physique: some View {
      VStack {
         Picture(systemName: "globe")
            .imageScale(.massive)
            .foregroundStyle(.tint)
         Textual content("Good day, world!")
         Button(motion: {
            
            guard supervisor.isDeviceMotionAvailable else { return }
            
            supervisor.startDeviceMotionUpdates(to: queue) { (movement, error) in
               guard let movement else {
                  return
               }
               
               let currentAttitude = movement.perspective
               myAttitude = currentAttitude
               print("🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢")
               print("Perspective-Pitch: (myAttitude)")//  ←←← These Work Tremendous!
               print("Perspective-Pitch: (myAttitude.pitch.formatted(.quantity.precision(.fractionLength(5))))")
               print("Perspective-Yaw: (myAttitude.yaw.formatted(.quantity.precision(.fractionLength(5))))")
               print("Perspective-Roll: (myAttitude.roll.formatted(.quantity.precision(.fractionLength(5))))")
               print("🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴")
            }
         }, label: {
            Textual content("Examine Your Perspective")
               .foregroundStyle(.cyan)
         })
         Textual content("Perspective-Pitch: (myAttitude)") //  ←←← App Crashes Right here!
//         Textual content("Perspective-Pitch: (myAttitude.pitch.formatted(.quantity.precision(.fractionLength(5))))")
//         Textual content("Perspective-Yaw: (myAttitude.yaw.formatted(.quantity.precision(.fractionLength(5))))")
//         Textual content("Perspective-Roll: (myAttitude.roll.formatted(.quantity.precision(.fractionLength(5))))")
      }
      .padding()
   }
}

#Preview {
   ContentView(myAttitude: CMAttitude())
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments