我正在尝试访问苹果手表的陀螺仪。据我所知,它可以在 watchos 3 中使用。不幸的是我无法让它工作。它不断返回“陀螺仪不可用”,因此 MotionManager.isGyroAvailable 始终为 false。这是我的代码。任何帮助,将不胜感激。
import WatchKit
import Foundation
import CoreMotion
class InterfaceController: WKInterfaceController {
let motionManager = CMMotionManager()
override func awake(withContext context: Any?) {
super.awake(withContext: context)
motionManager.gyroUpdateInterval = 0.1
motionManager.accelerometerUpdateInterval = 0.1
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
if (motionManager.isGyroAvailable == true) {
motionManager.startGyroUpdates(to: OperationQueue.current!, withHandler: { (data, error) -> Void in
guard let data = data else { return }
let rotationX = data.rotationRate.x
let rotationY = data.rotationRate.y
let rotationZ = data.rotationRate.z
// do you want to want to do with the data
print(rotationX)
print(rotationY)
print(rotationZ)
})
} else {
print("Gyro not available")
}
根据我的经验(虽然我在任何地方都找不到它的记录)原始陀螺仪数据在手表上不可用,只有处理后的数据。您可以使用以下方式访问处理后的数据CMMotionManager https://developer.apple.com/documentation/coremotion/cmmotionmanager method:
startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler: @escaping CMDeviceMotionHandler)
The CMDeviceMotion
处理程序中的对象具有详细的旋转数据,例如旋转速度 https://developer.apple.com/documentation/coremotion/cmdevicemotion/1615967-rotationrate,该文档指出它处理来自陀螺仪的数据。还有态度数据 https://developer.apple.com/documentation/coremotion/cmdevicemotion/1616050-attitude.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)