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

查看:118
本文介绍了简单的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.

创建 CMDeviceMotionHandler ,如 startDeviceMotionUpdatesToQueue: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天全站免登陆