CMMotionManager object is the gateway to the motion services provided by iOS. These services provide an app with accelerometer data, rotation-rate data, magnetometer data, and other device-motion data such as attitude. These types of data originate with a device’s accelerometers and (on some models) its magnetometer and gyroscope.
- iOS 4.0+
- watchOS 2.0+
After creating an instance of
CMMotionManager, an app can use it to receive four types of motion: raw accelerometer data, raw gyroscope data, raw magnetometer data, and processed device-motion data (which includes accelerometer, rotation-rate, and attitude measurements). The processed device-motion data provided by Core Motion’s sensor fusion algorithms gives the device’s attitude, rotation rate, calibrated magnetic fields, the direction of gravity, and the acceleration the user is imparting to the device.
An app can take one of two approaches when receiving motion data, by handling it at specified update intervals or periodically sampling the motion data. With both of these approaches, the app should call the appropriate stop method (
stopDeviceMotionUpdates()) when it has finished processing accelerometer, rotation-rate, magnetometer, or device-motion data.
Handling Motion Updates at Specified Intervals
To receive motion data at specific intervals, the app calls a “start” method that takes an operation queue (instance of
OperationQueue) and a block handler of a specific type for processing those updates. The motion data is passed into the block handler. The frequency of updates is determined by the value of an “interval” property.
Accelerometer. Set the
accelerometerUpdateIntervalproperty to specify an update interval. Call the
startAccelerometerUpdates(to:withHandler:)method, passing in a block of type
CMAccelerometerHandler. Accelerometer data is passed into the block as
Gyroscope. Set the
gyroUpdateIntervalproperty to specify an update interval. Call the
startGyroUpdates(to:withHandler:)method, passing in a block of type
CMGyroHandler. Rotation-rate data is passed into the block as
Magnetometer. Set the
magnetometerUpdateIntervalproperty to specify an update interval. Call the
startMagnetometerUpdates(to:withHandler:)method, passing a block of type
CMMagnetometerHandler. Magnetic-field data is passed into the block as
Device motion. Set the
deviceMotionUpdateIntervalproperty to specify an update interval. Call the
startDeviceMotionUpdates(to:withHandler:)method, passing in a block of type
CMDeviceMotionHandler. With the former method (new in iOS 5.0), you can specify a reference frame to be used for the attitude estimates. Rotation-rate data is passed into the block as
Periodic Sampling of Motion Data
To handle motion data by periodic sampling, the app calls a “start” method taking no arguments and periodically accesses the motion data held by a property for a given type of motion data. This approach is the recommended approach for apps such as games. Handling accelerometer data in a block introduces additional overhead, and most game apps are interested only the latest sample of motion data when they render a frame.
Device motion. Call the
startDeviceMotionUpdates()method to begin updates and periodically access
CMDeviceMotionobjects by reading the
startDeviceMotionUpdates(using:)method (new in iOS 5.0) lets you specify a reference frame to be used for the attitude estimates.
Hardware Availability and State
If a hardware feature (for example, a gyroscope) is not available on a device, calling a start method related to that feature has no effect. You can find out whether a hardware feature is available or active by checking the appropriate property; for example, for gyroscope data, you can check the value of the