Motion

iOS devices have an accelerometer which can detect motion along three axes. Some recent devices also have a gyroscope which can detect rotation around three axes. And some have a compass (magnetometer) that can measure the magnetic field along three axes. The Motion Manager reference is here.

These offer many opportunities to enhance apps.

Accelerometer

The accelerometer can detect rapid motion as well as the steady pull of gravity.It is quite simple. Start it by giving it a time interval in seconds for regular updates and a delegate object,

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 5.0)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];

In the delegate object implement the only protocol method,

- (void)accelerometer:(UIAccelerometer *)accelerometer
                didAccelerate:(UIAcceleration *)acceleration
{
 //process the components: acceleration.x, acceleration.y, acceleration.z
}

Acceleration is reported in units of G, that is, 1.0 is the normal force of gravity.

In a game you might want to detect rapid motion and ignore gravity. In another application you might want to suppress rapid motion and only pay attention to gravity. These can be done by processing the stream of data through a high-pass or low-pass filter, respectively. Apple's sample code shows how to do this. UIAccelerometer documentation. See their projects AccelerometerGraph and BubbleLevel.

Shake Events

Some designers like to detect when the device is shaken and use that to reset the state or something similar. One could do that, starting with the code above, but recent version of the OS have included a shake event. The OS does the detection and reports it to a chosen object. Since it is reported as a system event, it uses the responder mechanism. Setting it up is only slightly tricky. You must,

1. Create a class which is a subclass of UIResponder (a subclass of UIView will work).

2. Put it in a view controller and arrange to add it or remove it from the responder chain as the view appears or disappears.

- (void) viewWillAppear:(BOOL)animated
{
 [shakeView  becomeFirstResponder];
 [super viewWillAppear:animated];
}



- (void) viewWillDisappear:(BOOL)animated
{
 [shakeView resignFirstResponder];
 [super viewWillDisappear:animated];
}   

3. In the subclass, implement

- (BOOL)canBecomeFirstResponder
       { return YES; }
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event

{ // Shake detected, do what you choose
}


See the sample code in ShakeDetector.

Magnetometer

The magnetometer, which does not exist on all iOS devices, measures the strength of the magnetic field along three axes. It is a part of Location Services. It can deliver the values of the magnetic field along the axes, and it can deliver direction information as if it were a compass. Notice that the device can distinguish between magnetic north and true north. It must do this by maintaining a matrix of magnetic declination and using the longitude and latitude to find the local value.

Here is a project that uses the accelerometer and magnetometer, plus a lot of OpenGL, to show the gravity and magnetic fields.

See also Apple's Teslameter.

The page was last updated Friday, May 11, 2012 8:55 AM