我正试图访问苹果手表的陀螺仪。从我读到的内容可以看到它3.不幸的是我无法让它工作。它不断回来“陀螺仪不可用”,所以motionManager.isGyroAvailable总是假的。这是我的代码。任何帮助,将不胜感激。
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")
}
答案 0 :(得分:2)
根据我的经验(虽然我无法在任何地方找到它),手表上没有原始陀螺仪数据,只有经过处理的数据。您可以使用CMMotionManager方法访问已处理的数据:
startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler: @escaping CMDeviceMotionHandler)
处理程序中的CMDeviceMotion
对象具有详细的旋转数据,例如rotation rate,其中的文档表明它是来自陀螺仪的处理数据。还有attitude data。