user4788711
user4788711

Reputation:

Detect if the iPhone movement changes direction?

In my app the iPhone is moved in two cases:

  1. Moved in lineare direction

  2. Moved in a arc direction

How can I detect whether the direction of the movement changes the direction?

Upvotes: 1

Views: 405

Answers (1)

atreat
atreat

Reputation: 4413

You'll have to pull in the CoreMotion framework and start the device accelerometer and/or gyroscope. CoreMotion Reference

What you'r looking to get is CMAccelerometerData. This is done by instantiating a CMMotionManager object and calling startAccelerometerUpdatesToQueue:withHandler:

Something like this:

CMMotionManager *manager = [[CMMotionManager alloc] init];
manager.accelerometerUpdateInterval = 0.5;  // half a second

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[manager startAccelerometerUpdatesToQueue:queue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
    double x = accelerometerData.acceleration.x;
    double y = accelerometerData.acceleration.y;
    double z = accelerometerData.acceleration.z;

    // post back to main queue to update UI
    dispatch_async(dispatch_get_main_queue(), ^{           

    });
}]; 

You'll need to use some good old-fashioned geometry to detect arcs vs. lines.

Upvotes: 1

Related Questions