【发布时间】:2026-01-07 11:50:01
【问题描述】:
我对这个问题的标题措辞很怀疑,但我认为这就是重点。
我一直试图只读取 WatchKit 上的 CoreMotion 数据,但事实证明,我无法让 startDeviceMotionUpdatesToQueue 工作,我的处理程序从未被调用。
我尝试在自定义后台线程 (NSOperationQueue()) 中运行,但仍然没有成功。
我在真正的 Apple Watch 上调试,而不是模拟器。
在我的WKInterfaceController:
let manager = CMMotionManager()
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
let communicator = SessionDelegate()
manager.deviceMotionUpdateInterval = 1 / 60
manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) {
(motionerOp: CMDeviceMotion?, errorOp: NSError?) -> Void in
print("got into handler")
guard let motion = motionerOp else {
if let error = errorOp {
print(error.localizedDescription)
}
assertionFailure()
return
}
print("passed guard")
let roll = motion.attitude.roll
let pitch = motion.attitude.pitch
let yaw = motion.attitude.yaw
let attitudeToSend = ["roll": roll, "pitch": pitch, "yaw": yaw]
communicator.send(attitudeToSend)
}
print("normal stack")
}
输出是
normal stack
normal stack
(是的,两次!我也不知道为什么,但这不是重点,一定是我做错了另一件事)
我在这里发布这个是因为我不知道在哪里查看,这太疯狂了。
【问题讨论】:
标签: ios watchkit core-motion