我不是 UIAcceleration 方面的专家,但这里是示例代码中的一个示例。希望能帮助到你。
#define kAccelerometerFrequency 50.0 //Hz
#define kFilteringFactor 0.1
UIAccelerationValue gravX;
UIAccelerationValue gravY;
UIAccelerationValue gravZ;
gravX = (acceleration.x * kFilteringFactor) + (gravX * (1.0 - kFilteringFactor));
gravY = (acceleration.y * kFilteringFactor) + (gravY * (1.0 - kFilteringFactor));
gravZ = (acceleration.z * kFilteringFactor) + (gravZ * (1.0 - kFilteringFactor));
UIAccelerationValue accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (gravX * (1.0 - kFilteringFactor)) );
UIAccelerationValue accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (gravY * (1.0 - kFilteringFactor)) );
UIAccelerationValue accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (gravZ * (1.0 - kFilteringFactor)) );
accelX *= 9.81f;
accelY *= 9.81f;
accelZ *= 9.81f;
accelX = [self tendToZero:accelX];
accelY = [self tendToZero:accelY];
accelZ = [self tendToZero:accelZ];
UIAccelerationValue vector = sqrt(pow(accelX,2)+pow(accelY,2)+pow(accelZ, 2));
UIAccelerationValue acce = vector - prevVelocity;
UIAccelerationValue velocity = (((acce - prevAcce)/2) * (1/kAccelerometerFrequency)) + prevVelocity;
NSLog(@"X %g Y %g Z %g, Vector %g, Velocity %g",accelX,accelY,accelZ,vector,velocity);