关于在swiftUI中使用CMMotion的一些问题



我想写一个代码来检索Iphone的运动数据在Swift的MVVM模式,但我得到一个错误在我的模型文件的第15行:

转义闭包捕获变化的'self'参数<<

import Foundation
import CoreMotion
struct recoderModel {
private let motion = CMMotionManager()
private let queue = OperationQueue()
private(set) var attitude: Attitude
lazy var handler: CMDeviceMotionHandler = {(motionData: CMDeviceMotion?, error: Error?) -> Void in
storeMotionData(data: motionData, error: error)
}
mutating func startQueuedUpdates() {
if motion.isDeviceMotionAvailable {
motion.deviceMotionUpdateInterval = 1.0 / 60.0
motion.showsDeviceMovementDisplay = true
motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical,
to: self.queue, withHandler: handler)
}
}
mutating func storeMotionData(data: CMDeviceMotion?, error: Error?) {
// Make sure the data is valid before accessing it.
if let validData = data {
// Get the attitude relative to the magnetic north reference frame.
let roll = validData.attitude.roll
let pitch = validData.attitude.pitch
let yaw = validData.attitude.yaw

attitude.pitch = pitch
attitude.roll = roll
attitude.yaw = yaw
// Use the motion data in your app.
}
}
struct Attitude {
var pitch: Double = 0.0
var roll: Double = 0.0
var yaw: Double = 0.0
}
}

你试过改变

motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical,
to: self.queue, withHandler: handler)

motion.startDeviceMotionUpdates(using: .xMagneticNorthZVertical,
to: self.queue, withHandler: self.handler)

最新更新