1

我已经编写了这段代码来从 xcode 中的 UIAccelerometer 获取 G 值。你能告诉我它是否正确吗?

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    float gValue = sqrt(acceleration.x*acceleration.x+acceleration.y*acceleration.y+acceleration.z*acceleration.z);
    [gLabel setText:[NSString stringWithFormat:@"%.2f",gValue]];
}

请帮助我

4

1 回答 1

2

If G value is supposed to be Gravitational acceleration then this code is incorrect.

Problems:

  1. It could work but only if the device is not moving and the only acceleration is created by the gravitational force of the Earth. In all other cases the equation is incorrect.

  2. UIAcceleration values are scaled by G. That means that when device is idle, the values will be, for example (0, -1.0, 0) and your result is 1.0.

Also note that UIAccelerometer was deprecated and superseded by "Core Motion" framework.

EDIT: Use [CMDeviceMotion gravity].

于 2012-09-07T09:49:02.713 回答