iPhone加速度计方向 [英] iPhone Accelerometer direction

查看:301
本文介绍了iPhone加速度计方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,其中iPhone在移动(剑)时会播放不同的声音。
我想知道iPhone何时向左,向右,向上或向下移动(剑移动)。
如何实现呢?
我正在测试加速度计并编写了此代码,但是如果向左或向右移动它会返回相同的值。我怎么知道iPhone朝哪个方向移动?

I am developing an application in which iPhone will play different sounds when moved (sword). I want to know when iPhone has moved left, right, up or down (sword movement). How can I implement this? I was testing the accelerometer and wrote this code, but it returns the same values if moved left or right. How can I know in which direction is the iPhone moved?

代码:

- (void)accelerometer:(UIAccelerometer *)accelerometer
        didAccelerate:(UIAcceleration *)acceleration
{
    double const kThreshold = 1.0;

    if (fabsf(acceleration.x) > kThreshold) {
        NSLog(@"X+");
    }
}


推荐答案

左右和右都使用加速度。x

Left and right both use acceleration.x

我不确定哪个是负x,另一个是正x。

I'm not sure which is which but one is negative x and the other is positive x.

上下左右相同。

它们都使用相同的轴(x,y或z),但一个方向为正

They all use the same axis (x, y or z) but one direction is positive change and the other is negative change.

通过使用fabsf,您可以消除运动的负面影响。

By using fabsf you are removing the negative side of the movement.

您需要...

if (acceleration.x > kThreshold) {
    NSLog(@"X+");
} else if (acceleration.x < kThreshold * -1) {
    NSLog(@"X-");
}

或其他。

这篇关于iPhone加速度计方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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