Skip to content

TMSFMXNativeCMMotionManager

Usage

The TMSFMXNativeCMMotionManager component 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.

Methods

Methods name Description
AccelerometerActive Verify if the accelerometer is active. (Has an active reading between starting and stopping).
AccelerometerAvailable Verify if the accelerometer is available.
AccelerometerData Returns the most recent accelerometer data.
DeviceMotion Returns the most recent device motion data.
DeviceMotionActive Verify if the device motion is active. (Has an active reading between starting and stopping).
DeviceMotionAvailable Verify if the device motion is active. (Has an active reading between starting and stopping).
GyroActive Verify if the gyroscope is active. (Has an active reading between starting and stopping).
GyroAvailable Verify if the gyroscope is available.
GyroData Returns the most recent gyroscope data.
MagnetometerActive Verify if the magnetometer is active. (Has an active reading between starting and stopping).
MagnetometerAvailable Verify if the magnetometer is available.
MagnetometerData Returns the most recent magnetometer data.
MotionManager A reference to the native CMMotionManager class.
StartAccelerometerUpdates Starts monitoring accelerometer data changes.
StartDeviceMotionUpdates Starts monitoring device motion changes.
StartGyroUpdates Starts monitoring gyroscope data changes.
StartMagnetometerUpdates Starts monitoring for magnetometer data changes.
StopAccelerometerUpdates Stops monitoring accelerometer data changes.
StopDeviceMotionUpdates Stops monitoring device motion changes.
StopGyroUpdates Stops monitoring gyroscope data changes.
StopMagnetometerUpdates Stops monitoring for magnetometer data changes.

Properties

Properties name Description
AccelerometerUpdateInterval The interval in seconds for providing updates from the accelerometer.
DeviceMotionUpdateInterval The interval in seconds for providing updates from the device motion.
GyroUpdateInterval The interval in seconds for providing updates from the gyroscope.
MagnetometerUpdateInterval The interval in seconds for providing updates from the magnetometer.

Events

Events name Description
OnAccelerometerError Event called when an error occurred during accelerometer data changes.
OnDeviceMotionError Event called when an error occurred during device motion changes.
OnGetAccelerometerData Event called when data from the accelerometer changes.
OnGetDeviceMotion Event called when device motion changes.
OnGetGyroData Event called when data from the gyroscope changes.
OnGetMagnetometerData Event called when data from the magnetometer changes.
OnGyroError Event called when an error occurred during gyroscope data changes.
OnMagnetometerError Event called when an error occured during magnetometer data changes.

Sample with Device Motion

The code below verifies if Device Motion is available and then executes the StartDeviceMotionUpdates method which then handles the data through an anonymous method which positions the Ellipse base on the TTMSFMXNativeCMDeviceMotion data record. An alternative would be the move the code that updates the Ellipse inside the OnGetDeviceMotion event.

if TMSFMXNativeCMMotionManager1.DeviceMotionAvailable then
begin
  TMSFMXNativeCMMotionManager1.StartDeviceMotionUpdates(
  procedure (AData: TTMSFMXNativeCMDeviceMotion)
  begin
    Ellipse1.Position.X := Max(0,Min(Panel1.Width - Ellipse1.Width, Ellipse1.Position.X + (AData.Attitude.Roll * 20)));
    Ellipse1.Position.Y := Max(0,Min(Panel1.Height - Ellipse1.Height, Ellipse1.Position.Y + (AData.Attitude.Pitch * 20)));
    if PtInRect(RectF(Rectangle1.Position.X, Rectangle1.Position.Y, Rectangle1.Position.X + Rectangle1.Width,
      Rectangle1.Position.Y + Rectangle1.Height), PointF(Ellipse1.Position.X, Ellipse1.Position.Y)) then
      Label2.Text := inttostr(strtoint(Label2.Text) + 1)
    else
      Label2.Text := '0';
  end
  );
end
else
  ShowMessage('Device Motion is not available on this device');