Class

CMMotionManager

A 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.

Overview

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.

Symbols

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.

Determining Whether the Accelerometer Is Active and Available

var isAccelerometerActive: Bool

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

var isAccelerometerAvailable: Bool

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

Accessing Accelerometer Data

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.

Determining Whether the Gyroscope Is Active and Available

var isGyroActive: Bool

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

var isGyroAvailable: Bool

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

Accessing Gyroscope Data

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.

Determining Whether the Magnetometer is Active and Available

var isMagnetometerActive: Bool

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

var isMagnetometerAvailable: Bool

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

Accessing Magnetometer Data

var magnetometerData: CMMagnetometerData?

The latest sample of magnetometer data.

Managing the Device Movement Display

var showsDeviceMovementDisplay: Bool

Controls whether the device-movement display is shown.

Managing Device Motion Updates

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.

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.

Determining Whether the Device Motion Hardware Is Active and Available

var isDeviceMotionActive: Bool

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

var isDeviceMotionAvailable: Bool

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

Accessing Device Motion Data

var deviceMotion: CMDeviceMotion?

The latest sample of device-motion data.

Constants

CMAccelerometerHandler

The type of block callback for handling accelerometer data.

CMGyroHandler

The type of block callback for handling gyroscope data.

CMMagnetometerHandler

The type of block callback for handling magnetometer data.

CMDeviceMotionHandler

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

Core Motion Error Domain

The error domain for Core Motion.

CMError

Relationships

Inherits From

Conforms To