Class

CMMotionManager

The object for starting and managing motion services.

Overview

You use a CMMotionManager object to access accelerometer data, rotation-rate data, magnetometer data, and other device-motion data. These types of data originate with a device’s accelerometers and (on some models) its magnetometer and gyroscope.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 (stopAccelerometerUpdates(), stopGyroUpdates(), stopMagnetometerUpdates(), and 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.

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.

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.

Topics

Determining the Availability of Services

var isDeviceMotionAvailable: Bool

A Boolean value that indicates whether the device-motion service is available on the device.

var isAccelerometerAvailable: Bool

A Boolean value that indicates whether an accelerometer is available on the device.

var isGyroAvailable: Bool

A Boolean value that indicates whether a gyroscope is available on the device.

var isMagnetometerAvailable: Bool

A Boolean value that indicates whether a magnetometer is available on the device.

Determining Which Services Are Active

var isDeviceMotionActive: Bool

A Boolean value that determines whether the app is receiving updates from the device-motion service.

var isAccelerometerActive: Bool

A Boolean value that indicates whether accelerometer updates are currently happening.

var isGyroActive: Bool

A Boolean value that determines whether gyroscope updates are currently happening.

var isMagnetometerActive: Bool

A Boolean value that determines whether magnetometer updates are currently happening.

Managing Device Motion Updates

var showsDeviceMovementDisplay: Bool

Controls whether the device-movement display is shown.

var deviceMotionUpdateInterval: TimeInterval

The interval, in seconds, for providing device-motion updates to the block handler.

func startDeviceMotionUpdates(using: CMAttitudeReferenceFrame, to: OperationQueue, withHandler: CMDeviceMotionHandler)

Starts device-motion updates on an operation queue and using a specified reference frame and block handler.

func startDeviceMotionUpdates(to: OperationQueue, withHandler: CMDeviceMotionHandler)

Starts device-motion updates on an operation queue and using a specified block handler.

func startDeviceMotionUpdates(using: CMAttitudeReferenceFrame)

Starts device-motion updates using a reference frame but without a block handler.

func startDeviceMotionUpdates()

Starts device-motion updates without a block handler.

func stopDeviceMotionUpdates()

Stops device-motion updates.

var deviceMotion: CMDeviceMotion?

The latest sample of device-motion data.

Managing Accelerometer Updates

var accelerometerUpdateInterval: TimeInterval

The interval, in seconds, for providing accelerometer updates to the block handler.

func startAccelerometerUpdates(to: OperationQueue, withHandler: CMAccelerometerHandler)

Starts accelerometer updates on an operation queue and with a specified handler.

func startAccelerometerUpdates()

Starts accelerometer updates without a handler.

func stopAccelerometerUpdates()

Stops accelerometer updates.

var accelerometerData: CMAccelerometerData?

The latest sample of accelerometer data.

Managing Gyroscope Updates

var gyroUpdateInterval: TimeInterval

The interval, in seconds, for providing gyroscope updates to the block handler.

func startGyroUpdates(to: OperationQueue, withHandler: CMGyroHandler)

Starts gyroscope updates on an operation queue and with a specified handler.

func startGyroUpdates()

Starts gyroscope updates without a handler.

func stopGyroUpdates()

Stops gyroscope updates.

var gyroData: CMGyroData?

The latest sample of gyroscope data.

Managing Magnetometer Updates

var magnetometerUpdateInterval: TimeInterval

The interval, in seconds, at which the system delivers magnetometer data to the block handler.

func startMagnetometerUpdates(to: OperationQueue, withHandler: CMMagnetometerHandler)

Starts magnetometer updates on an operation queue and with a specified handler.

func startMagnetometerUpdates()

Starts magnetometer updates without a block handler.

func stopMagnetometerUpdates()

Stops magnetometer updates.

var magnetometerData: CMMagnetometerData?

The latest sample of magnetometer data.

Accessing Attitude Reference Frames

var attitudeReferenceFrame: CMAttitudeReferenceFrame

Returns either the reference frame currently being used or the default attitude reference frame

class func availableAttitudeReferenceFrames()

Returns a bitmask specifying the available attitude reference frames on the device.

Constants

typealias CMAccelerometerHandler

The type of block callback for handling accelerometer data.

typealias CMGyroHandler

The type of block callback for handling gyroscope data.

typealias CMMagnetometerHandler

The type of block callback for handling magnetometer data.

typealias CMDeviceMotionHandler

The type of block callback for handling device-motion data.

let CMErrorDomain: String

The error domain for Core Motion.

struct CMError

Defines a data type for a ColorSync Manager result code.

Relationships

Inherits From

Conforms To

See Also

First Steps

class CMLogItem

The base class for all motion-related data objects.