我正在尝试访问 Apple Watch 的陀螺仪。从我读到的内容可以在 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")
}
最佳答案
根据我的经验(虽然我无法在任何地方找到它)原始陀螺仪数据在 watch 上不可用,只有处理过的数据。您可以使用 CMMotionManager 访问处理后的数据。方法:
startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler: @escaping CMDeviceMotionHandler)
CMDeviceMotion
处理程序中的对象具有详细的旋转数据,例如 rotation rate ,此文档说明它已处理来自陀螺仪的数据。还有attitude data .
关于core-motion - CoreMotion陀螺仪苹果 watch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42650961/