iOS-开始接收陀螺仪更新
我使用 CMMotionManager 来访问 iOS 的陀螺仪数据。我看到有两种方法:
startGyroUpdates
startGyroUpdatesToQueue:withHandler:
开始接收陀螺仪更新。我们如何区分调用这两个方法。什么情况下可以调用它们中的任何一个?其中一个相对于另一个有什么重要意义吗?
任何帮助表示赞赏,
I make use of CMMotionManager in order to access the gyroscope data for iOS. I see that there are 2 methods :
startGyroUpdates
startGyroUpdatesToQueue:withHandler:
to start receiving the gyro updates. How can we differentiate between calling these two methods. What are the situations when the either of them can be called? IS there any significance of one over the other?
Any help appreciated,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
队列用于保证所有事件都得到处理,即使您在 deviceMotionUpdateInterval 生成事件的速度比您可以实时处理。如果您不介意丢失事件,那么使用两者中的哪一个并不重要,只需丢弃它们即可。
相关的Apple文档是事件处理指南的核心运动部分:
这不是你的问题,但我想知道你是否想要原始的 x、y、z 旋转或更有用的俯仰、滚动、偏航。对于稍后使用 startDeviceMotionUpdatesToQueue:withHandler: 改为 startGyroUpdatesToQueue:withHandler:。
A queue is used to guarantee that all events are processed, even when the update interval you set in deviceMotionUpdateInterval is producing events at a faster rate than you can process in real time. If you don't mind missing events, it doesn't matter which one of the two you use, just discard them.
The relevant Apple doc is the Core Motion section of the Event Handling Guide:
It's not on your question, but I wonder if you want the raw x,y,z rotation or the more useful pitch,roll,yaw. For the later use startDeviceMotionUpdatesToQueue:withHandler: instead startGyroUpdatesToQueue:withHandler:.
编辑:查看汤米对此答案的评论。我对委托模式的假设是错误的。
我对
CMMotionManager
不是特别熟悉,但从命名来看我的猜测是:startGyroUpdates
通过在主线程上调用委托方法来提供陀螺仪更新。
startGyroUpdatesToQueue:withHandler:
通过调用给定队列上的处理程序块来提供陀螺仪更新。
第一个是使用委托的预块样式,第二个是基于 GCD 的块化版本。
Edit: See Tommy's comment on this answer. My assumption of the delegate pattern was wrong.
I'm not particularly familiar with
CMMotionManager
, but from the naming here's my guess:startGyroUpdates
Delivers gyroscope updates by invoking delegate methods on the main thread.
startGyroUpdatesToQueue:withHandler:
Delivers gyroscope updates by invoking the handler block on the given queue.
The first would be the pre-block style using delegates, and the second would be the blockified version based on GCD.