id | title | brief | samplecode | sdk | ||||||
---|---|---|---|---|---|---|---|---|---|---|
5081B5B0-0FC0-BFCC-B4F7-056D8FD9F82D |
Use CoreMotion with Accelerometer |
This recipe shows how to use the Core Motion framework to receive data from the accelerometer. |
|
Create an instance of CoreMotion.CMMotionManager
, and begin
listening to updates from the Accelerometer with the method
StartAccelerometerUpdates
:
The following code snippet will display the accelerometer data for each axis in UILabels:
motionManager = new CMMotionManager ();
motionManager.StartAccelerometerUpdates (NSOperationQueue.CurrentQueue, (data, error) =>
{
this.lblX.Text = data.Acceleration.X.ToString ("0.00000000");
this.lblY.Text = data.Acceleration.Y.ToString ("0.00000000");
this.lblZ.Text = data.Acceleration.Z.ToString ("0.00000000");
});
The first parameter, data
is a CMAccelerometerData instance holding the accelerometer data for the event.
The CoreMotion.CMMotionManager
class is a gateway to the motion
services provided by iOS. An application should only have a single instance of
the CMMotionManager
class.
The iOS simulator has no support for the features from Core Motion – the code may only be tested on a device.