我在试着拿到苹果手表的陀螺仪。据我所知,它在watchOS3中是可用的。不幸的是,我不能让它工作。它总是返回“陀螺仪不可用”,所以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")
}发布于 2017-10-12 21:47:17
根据我的经验(尽管我在任何地方都找不到文档),手表上没有原始的陀螺仪数据,只有经过处理的数据。您可以使用CMMotionManager方法访问处理后的数据:
startDeviceMotionUpdates(to queue: OperationQueue, withHandler handler: @escaping CMDeviceMotionHandler)
处理程序中的CMDeviceMotion对象有详细的旋转数据,例如rotation rate,文档说明它是从陀螺仪中处理的数据。还有attitude data。
https://stackoverflow.com/questions/42650961
复制相似问题