简单的 iPhone 运动检测
我需要检测陀螺仪/加速度计何时被激活一定量。基本上是检测设备何时移动。我对核心运动一无所知。
也许有人可以指导我入门教程或其他东西。
提前致谢。
I need to detect when the gyroscope / accelerometer is activated a certain amount. Basically to detect when there is movement of the device. I don't know anything about Core Motion.
Maybe someone can direct me to a starters tutorial or something.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你必须使用 Core Motion。好消息是,它对于您的问题域来说并不难使用。开始特别阅读事件处理指南处理已处理的设备运动数据部分。如果您只是想知道发生了轻微的动作,正如您所说,您可以省略 CMDeviceMotion.userAcceleration 上的旋转处理和窄信号处理。这是因为每次旋转也会产生加速度计信号。
创建 CMDeviceMotionHandler 如 startDeviceMotionUpdatesToQueue:withHandler:
您的 CMDeviceMotionHandler 应该执行类似以下操作:
基本上就是这样。请记住,每一次加速都会有一个对应的加速。这意味着,如果您施加力将设备向右移动(即加速),则会有对应的减速来停止运动并使设备静止在新位置。因此,对于每个动作,您的
if
条件都会变为 true 两次。I think you have to use Core Motion. The good news is that it is not that hard to use for your problem domain. Start reading the Event Handling Guide especially the section Handling Processed Device-Motion Data. If you are just interested in knowing that a slight motion was made, as you stated, you can omit rotation handling and narrow signal processing on CMDeviceMotion.userAcceleration. This is because every rotation results in accelerometer signals as well.
Create a CMDeviceMotionHandler as described in startDeviceMotionUpdatesToQueue:withHandler:
Your CMDeviceMotionHandler should do something like:
Basically that's it. Bear in mind that every acceleration will have a counterpart. That means, if you apply a force to move (i.e. accelerate) the device to the right, there will be a counterpart for deceleration to stop the motion and let that the device rest at the new position. So your
if
condition will become true twice for every single motion.在
viewDidAppear
中,成为第一响应者:并确保你可以成为第一响应者:
然后就可以实现运动检测了。
In
viewDidAppear
, become the first responder:And make sure you can be first responder:
Then you can implement the motion detection.