简单的 iPhone 运动检测

发布于 2024-10-21 02:59:48 字数 95 浏览 3 评论 0原文

我需要检测陀螺仪/加速度计何时被激活一定量。基本上是检测设备何时移动。我对核心运动一无所知。

也许有人可以指导我入门教程或其他东西。

提前致谢。

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

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

发布评论

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

评论(2

梦中的蝴蝶 2024-10-28 02:59:48

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

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

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 条件都会变为 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:

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
}

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.

云之铃。 2024-10-28 02:59:48

viewDidAppear中,成为第一响应者:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

并确保你可以成为第一响应者:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

然后就可以实现运动检测了。

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventTypeMotion){
        //there was motion
    }
}

In viewDidAppear, become the first responder:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

And make sure you can be first responder:

- (BOOL)canBecomeFirstResponder {
    return YES;
}

Then you can implement the motion detection.

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.subtype == UIEventTypeMotion){
        //there was motion
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文