Question

I have written this code to get the G value from UIAccelerometer in xcode. Could you tell me is it correct or not?

-(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]];
}

Kindly help me

Était-ce utile?

La solution

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].

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top