这段代码有什么问题以及如何在屏幕上显示数据

What's wrong with this code and how can I show the data on screen

我写这段代码是为了显示传感器数据并在 swiftui 中读取和显示它们,不幸的是它不起作用。有人能帮我吗 ?? swiftui 中有一些错误,我也有一些错误,但最重要的是首先获取传感器数据。 读取加速度计和陀螺仪数据的代码是否正确?

'''

  HStack{
                Button("Start") {
                  // start()
                             
                } .padding()
                Button("Stop") {
                  // start()
                             
                } .padding()
              
            }                      
        }       
    }
   func start(){
       
        self.motionManager.gyroUpdateInterval = 0.5
           motionManager.startGyroUpdates(to: OperationQueue.current!) { (data, error) in
            print(data as Any)
                 
             if let data = self.motionManager.gyroData {
                let xG = data.rotationRate.x
                let yG = data.rotationRate.y
                let zG = data.rotationRate.z
               
                self.appendReadingGyroscope(x: xG, y: yG, z: zG)
                // Use the gyroscope data in your app.
             }
     
    }
          
        
    self.motionManager.accelerometerUpdateInterval = 0.5
        motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { (data, error) in
               print(data as Any)
                  
          
            if let data = self.motionManager.accelerometerData {
                  let xA = data.acceleration.x
                  let yA = data.acceleration.y
                  let zA = data.acceleration.z
                  
                self.appendReadingAccelerometer(x: xA, y: yA, z: zA)
              
                  // Use the accelerometer data in your app.
               }
           }
       }
    func stop() {
         
             self.motionManager.stopGyroUpdates()
             self.motionManager.stopAccelerometerUpdates()
          }

'''

您的代码有几个问题。

首先,尝试将 MotionManagerContentView 分开,并在属性更新时使用 Combine 读取属性。

我建了一个MotionManagerclass。 MotionManagerclass 是一个 ObservableObject(组合),因为我们需要将 x、y 和 z 实现为 @Published 属性,以便在它们更新时在我们的 ContentView 中读取它们。

这是 class MotionManager :

class MotionManager: ObservableObject {
    // MotionManager use the ObservableObject Combine property.
    private var motionManager: CMMotionManager

    @Published
    var x: Double = 0.0
    @Published
    var y: Double = 0.0
    @Published
    var z: Double = 0.0
    // x, y and z use are Published so ContentView can read the values when they update.

    // init
    init() {
        self.motionManager = CMMotionManager()
        self.motionManager.magnetometerUpdateInterval = 0.5
        self.motionManager.startMagnetometerUpdates(to: .main) { (magnetometerData, error) in
            guard error == nil else {
                print(error!)
                return
            }

            if let magnetData = magnetometerData {
                self.x = magnetData.magneticField.x
                self.y = magnetData.magneticField.y
                self.z = magnetData.magneticField.z
            }

        }

    }
}

现在这是您的ContentView。它更干净。 如果你愿意,你可以在这里实现你的按钮。

在您的 ContentView 中,您现在需要实例化 MotionManager class 以便访问 x、y 和 z @Published 属性。

struct ContentView: View {
    
    @ObservedObject var motion: MotionManager
    // You need to instanciate your MotionManager class as an ObservedObject to use x, y and z when the update
    
    var body: some View {
        
        VStack {
                    Text("Magnetometer Data")
                    Text("X: \(motion.x)")
                    Text("Y: \(motion.y)")
                    Text("Z: \(motion.z)")
                }
    }
}

请注意,您需要将 MotionManager() 传递到预览代码和 WindowGroup 中才能编译: ContentView(motion: MotionManager())

我希望我的代码能帮助您了解您的代码出了什么问题。如果没有,请随时询问。

-------- 编辑

我实现了 start()stop() 功能。

//
//  ContentView.swift
//  todeletenow
//
//  Created by Théo Voglimacci on 29/09/2020.
//

import SwiftUI
import Combine
import CoreMotion

struct ContentView: View {
    
    @ObservedObject var motion: MotionManager
    // You need to instanciate your MotionManager class as an ObservedObject to use x, y and z when the update
    
    var body: some View {
        
        HStack {
            
            Button("Start") {
                motion.start()
                
            } .padding()
            
            Button("Stop") {
                motion.stop()
                
            } .padding()
            
        }
        
        VStack {
            Text("Magnetometer Data")
            Text("X: \(motion.x)")
            Text("Y: \(motion.y)")
            Text("Z: \(motion.z)")
        }
    }
}

class MotionManager: ObservableObject {
    // MotionManager use the ObservableObject Combine property.
    private var motionManager: CMMotionManager
    
    @Published
    var x: Double = 0.0
    @Published
    var y: Double = 0.0
    @Published
    var z: Double = 0.0
    // x, y and z use are Published so ContentView can read the values when they update.
    
    func start() {
        motionManager.startMagnetometerUpdates(to: .main) { (magnetometerData, error) in
            guard error == nil else {
                print(error!)
                return
            }
            
            if let magnetData = magnetometerData {
                self.x = magnetData.magneticField.x
                self.y = magnetData.magneticField.y
                self.z = magnetData.magneticField.z
            }
            
        }
    }
    
    func stop() {
        motionManager.stopMagnetometerUpdates()
    }
    
    // init
    init() {
        self.motionManager = CMMotionManager()
        self.motionManager.magnetometerUpdateInterval = 0.5
        self.motionManager.startMagnetometerUpdates(to: .main) { (magnetometerData, error) in
            guard error == nil else {
                print(error!)
                return
            }
            
            if let magnetData = magnetometerData {
                self.x = magnetData.magneticField.x
                self.y = magnetData.magneticField.y
                self.z = magnetData.magneticField.z
            }
            
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(motion: MotionManager())
    }
}