iOS-开始接收陀螺仪更新

发布于 2024-12-01 21:15:31 字数 220 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长发绾君心 2024-12-08 21:15:31

队列用于保证所有事件都得到处理,即使您在 deviceMotionUpdateInterval 生成事件的速度比您可以实时处理。如果您不介意丢失事件,那么使用两者中的哪一个并不重要,只需丢弃它们即可。

相关的Apple文档是事件处理指南的核心运动部分:

对于上述每种数据运动类型,CMMotionManager
类提供了两种获取运动数据的方法,一种是推送方法
和拉动方法:

  • 推。应用程序请求更新间隔并实现
    用于处理运动数据的(特定类型的)块;那么它
    开始更新该类型的运动数据,传递到 Core Motion
    操作队列和块。 Core Motion 提供每一项
    更新到块,该块作为操作队列中的任务执行。

  • 拉。应用程序开始更新一种运动数据,并且
    定期对运动数据的最新测量结果进行采样。

拉动方法是大多数应用程序的推荐方法,
尤其是游戏;它通常更有效并且需要更少
代码。推送方法适合数据收集
应用程序和类似的应用程序,不能错过示例
测量。

这不是你的问题,但我想知道你是否想要原始的 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:

For each of the data-motion types described above, the CMMotionManager
class offers two approaches for obtaining motion data, a push approach
and a pull approach:

  • Push. An application requests an update interval and implements a
    block (of a specific type) for handling the motion data; it then
    starts updates for that type of motion data, passing into Core Motion
    an operation queue as well as the block. Core Motion delivers each
    update to the block, which executes as a task in the operation queue.

  • Pull. An application starts updates of a type of motion data and
    periodically samples the most recent measurement of motion data.

The pull approach is the recommended approach for most applications,
especially games; it is generally more efficient and requires less
code. The push approach is appropriate for data-collection
applications and similar applications that cannot miss a sample
measurement.

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:.

会发光的星星闪亮亮i 2024-12-08 21:15:31

编辑:查看汤米对此答案的评论。我对委托模式的假设是错误的。

我对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文