Search code examples
iosiphonexcodeaccelerometer

How to exclude particular axis from accelerometer code?


I am using the accelerometer function in my project and I need to exclude certain axises from the code. I want to exclude Y & Z and only use X. Thanks

Here is the code I'm using.

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

    double const kThreshold = 1.5;
    //    double const kThreshold = 2.0;
    if ( fabsf(acceleration.x) > kThreshold
        || fabsf(acceleration.y) > kThreshold
        || fabsf(acceleration.z) > kThreshold)

Solution

  • If you just want to check for acceleration above kThreshold for the x-axis, then change:

    if ( fabsf(acceleration.x) > kThreshold
            || fabsf(acceleration.y) > kThreshold
            || fabsf(acceleration.z) > kThreshold)
    

    to:

    if ( fabsf(acceleration.x) > kThreshold)