DeviceMotionEvent Class Reference
| Inherits from | Event |
| Companion guide | |
| Availability | Available in iOS 4.2 and later. |
Overview
An instance of DeviceMotionEvent is created when significant change in motion occurs. The event object encapsulates the measurements of the interval, rotation rate, and acceleration of a device.
The accelerometer measures the sum of two acceleration vectors: gravity and user acceleration. User acceleration is the acceleration that the user imparts to the device. Because the system is able to track the device’s attitude using both the gyroscope and the accelerometer, it can differentiate between gravity and user acceleration.
You register for these types of events using the window’s addEventListener method by passing devicemotion as the event type.
Properties
acceleration
The acceleration that the user is giving to the device.
readonly attribute Acceleration acceleration
Discussion
The acceleration data are expressed in m/s^2 and use the following x, y, and z axis properties represented as doubles:
- x
In the plane of the screen, positive towards the right side of the screen.
- y
In the plane of the screen, positive towards the top of the screen.
- z
Perpendicular to the screen, positive out of the screen.
This property is null if the device cannot provide the user acceleration—that is, if the device does not have a gyroscope.
Use the accelerationIncludingGravity property to get the total acceleration.
Availability
- Available in iOS 4.2 and later.
accelerationIncludingGravity
The total acceleration of the device, which includes the user acceleration and the gravity.
readonly attribute Acceleration accelerationIncludingGravity
Discussion
The acceleration data is expressed in m/s^2 and use the x, y, and z axis properties described in acceleration. This property is never null because every Apple device has an accelerometer.
Use the acceleration property to get the user acceleration only.
Availability
- Available in iOS 4.2 and later.
interval
The interval in milliseconds since the last device motion event.
readonly attribute double interval
Availability
- Available in iOS 4.2 and later.
rotationRate
The rotation rate of the device.
readonly attribute RotationRate rotationRate
Discussion
The RotationRate object specifies the device’s rate of rotation around three axes. It contains alpha, beta, and gamma properties represented as doubles as described in DeviceOrientationEvent Class Reference.
This property is null if the device cannot provide the rate of rotation—that is, if the device does not have a gyroscope.
Availability
- Available in iOS 4.2 and later.
Methods
initDeviceMotionEvent
Initializes a new device motion event.
Parameters
- type
The type of event. Pass
devicemotion.- bubbles
Indicates whether this event is a bubbling event.
- cancelable
Indicates whether this event can be canceled.
- acceleration
The acceleration that the user is giving to the device. The
Accelerationobject hasx,y, andzproperties represented as doubles.- accelerationIncludingGravity
The total acceleration of the device, which includes the user acceleration and the gravity. The
Accelerationobject hasx,y, andzproperties represented as doubles.- rotationRate
The rotation rate of the device. The
RotationRateobject hasalpha,beta, andgammaproperties represented as doubles.- interval
The interval in milliseconds since the last time this event was fired.
Availability
- Available in iOS 4.2 and later.
© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-06-06)