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.

Tasks

Creating Motion Events

Getting Motion Event Properties

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

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

interval

The interval in milliseconds since the last device motion event.

readonly attribute double interval
Availability

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

Methods

initDeviceMotionEvent

Initializes a new device motion event.

void initDeviceMotionEvent (in DOMString type, in boolean bubbles, in boolean cancelable, in Acceleration acceleration, in Acceleration accelerationIncludingGravity, in RotationRate rotationRate, in double interval);
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 Acceleration object has x, y, and z properties represented as doubles.

accelerationIncludingGravity

The total acceleration of the device, which includes the user acceleration and the gravity. The Acceleration object has x, y, and z properties represented as doubles.

rotationRate

The rotation rate of the device. The RotationRate object has alpha, beta, and gamma properties represented as doubles.

interval

The interval in milliseconds since the last time this event was fired.

Availability
  • Available in iOS 4.2 and later.