CMMotionManager
The object for starting and managing motion services.
Declaration
class CMMotionManagerMentioned in
Overview
Use a CMMotionManager object to start the services that report movement detected by the device’s onboard sensors. Use this object to receive four types of motion data:
Accelerometer data, indicating the instantaneous acceleration of the device in three dimensional space.
Gyroscope data, indicating the instantaneous rotation around the device’s three primary axes.
Magnetometer data, indicating the device’s orientation relative to Earth’s magnetic field.
Device-motion data, indicating key motion-related attributes such as the device’s user-initiated acceleration, its attitude, rotation rates, orientation relative to calibrated magnetic fields, and orientation relative to gravity. Core Motion’s sensor fusion algorithms provide this data.
The processed device-motion data gives the device’s attitude, rotation rate, calibrated magnetic fields, the direction of gravity, and the amount of acceleration the user contributes to the device.
You can receive live sensor data at a specified update interval, or you can let the sensors collect data and store it for retrieval later. With both of these approaches, call the appropriate stop method (stopAccelerometerUpdates(), stopGyroUpdates(), stopMagnetometerUpdates(), and stopDeviceMotionUpdates()) when you no longer need the data.
Receive regular motion updates
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 accelerometerUpdateInterval property 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 CMAccelerometerData objects.
Gyroscope. Set the gyroUpdateInterval property 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 CMGyroData objects.
Magnetometer. Set the magnetometerUpdateInterval property 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 CMMagnetometerData objects.
Device motion. Set the deviceMotionUpdateInterval property to specify an update interval. Call the startDeviceMotionUpdates(using:)or startDeviceMotionUpdates(using:to:withHandler:) or startDeviceMotionUpdates(to:withHandler:) method, passing in a block of type CMDeviceMotionHandler. With the former method, you can specify a reference frame to be used for the attitude estimates. Rotation-rate data is passed into the block as CMDeviceMotion objects.
Sample motion data periodically
To sample motion data periodically, start a motion service using a method that takes no parameters and periodically access the properties of the CMMotionManager. 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 only interested in the latest sample of motion data when they render a frame.
Accelerometer. Call startAccelerometerUpdates() to begin updates and periodically access CMAccelerometerData objects by reading the accelerometerData property.
Gyroscope. Call startGyroUpdates() to begin updates and periodically access CMGyroData objects by reading the gyroData property.
Magnetometer. Call startMagnetometerUpdates() to begin updates and periodically access CMMagnetometerData objects by reading the magnetometerData property.
Device motion. Call the startDeviceMotionUpdates(using:) or startDeviceMotionUpdates() method to begin updates and periodically access CMDeviceMotion objects by reading the deviceMotion property. The startDeviceMotionUpdates(using:) method lets you specify a reference frame for the attitude estimates.
Determine 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 isGyroAvailable or isGyroActive properties.
Identify the coordinate axes of the device
To interpret accelerometer, gyroscope, or attitude information, you need to know the orientation of the device’s coordinate axes. The following illustration shows the positive x-axis, positive y-axis, and positive z-axis for motion-capable Apple devices.
[Image]
Topics
Determining the Availability of Services
Determining Which Services Are Active
Managing Device Motion Updates
showsDeviceMovementDisplaydeviceMotionUpdateIntervalstartDeviceMotionUpdates(using:to:withHandler:)startDeviceMotionUpdates(to:withHandler:)startDeviceMotionUpdates(using:)startDeviceMotionUpdates()stopDeviceMotionUpdates()deviceMotionCMDeviceMotionHandler
Managing Accelerometer Updates
accelerometerUpdateIntervalstartAccelerometerUpdates(to:withHandler:)startAccelerometerUpdates()stopAccelerometerUpdates()accelerometerDataCMAccelerometerHandler
Managing Gyroscope Updates
gyroUpdateIntervalstartGyroUpdates(to:withHandler:)startGyroUpdates()stopGyroUpdates()gyroDataCMGyroHandler
Managing Magnetometer Updates
magnetometerUpdateIntervalstartMagnetometerUpdates(to:withHandler:)startMagnetometerUpdates()stopMagnetometerUpdates()magnetometerDataCMMagnetometerHandler