如何解决此代码有什么问题,如何在屏幕上显示数据
我编写了这段代码来显示传感器数据,并以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()
}
'''
解决方法
您的代码有几个问题。
首先,尝试将MotionManager
与ContentView
分开,并使用Combine
在属性更新时读取它们。
我建立了一个MotionManager
类。
MotionManager
类是一个ObservableObject(Combine),因为我们需要将x,y和z实现为@Published属性,以便在它们更新时在我们的ContentView
中读取它们。
这是课程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
类以访问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())
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。