简单的 iPhone 运动检测 [英] Simple iPhone motion detect

查看:28
本文介绍了简单的 iPhone 运动检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测陀螺仪/加速度计何时被激活到一定程度.基本上是检测设备何时移动.我对 Core Motion 一无所知.

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.

提前致谢.

推荐答案

我认为您必须使用 Core Motion.好消息是它并不难用于您的问题域.开始阅读事件处理指南处理已处理的设备运动数据部分.如果您只是想知道做了一个轻微的运动,正如您所说,您可以省略 CMDeviceMotion.userAcceleration 上的旋转处理和窄信号处理.这是因为每次旋转都会产生加速度计信号.

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.

创建一个 CMDeviceMotionHandlerstartDeviceMotionUpdatesToQueue:withHandler:您的 CMDeviceMotionHandler 应该执行以下操作:

Create a CMDeviceMotionHandler as described in startDeviceMotionUpdatesToQueue:withHandler: Your CMDeviceMotionHandler should do something like:

float accelerationThreshold = 0.2; // or whatever is appropriate - play around with different values
CMAcceleration userAcceleration = deviceMotion.userAcceleration;
if (fabs(userAcceleration.x) > accelerationThreshold) 
    || fabs(userAcceleration.y) > accelerationThreshold
    || fabs(userAcceleration.z) > accelerationThreshold) {
    // enter code here
}

基本上就是这样.请记住,每个加速都会有一个对应项.这意味着,如果您施加一个力将设备向右移动(即加速),则会有一个对应的减速来停止运动并让设备停在新位置.因此,您的 if 条件对于每个动作都会变为真两次.

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.

这篇关于简单的 iPhone 运动检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆