iOS Guides | Samples

MonoTouch.UIKit.UIAccelerometer Class

A class that encapsulates the onboard hardware accelerometer and generates UIAcceleration objects.

See Also: UIAccelerometer

Syntax

[MonoTouch.Foundation.Register("UIAccelerometer", true)]
[MonoTouch.ObjCRuntime.Deprecated(MonoTouch.ObjCRuntime.PlatformName.iOS, 5, 0, MonoTouch.ObjCRuntime.PlatformArchitecture.None, "Use 'CoreMotion' instead.")]
[MonoTouch.ObjCRuntime.Introduced(MonoTouch.ObjCRuntime.PlatformName.iOS, 2, 0, MonoTouch.ObjCRuntime.PlatformArchitecture.None, "Use 'CoreMotion' instead.")]
[MonoTouch.ObjCRuntime.Unavailable(MonoTouch.ObjCRuntime.PlatformName.TvOS, MonoTouch.ObjCRuntime.PlatformArchitecture.All, null)]
public class UIAccelerometer : NSObject

Remarks

The application developer must always use the UIAccelerometer.SharedAccelerometer property to access the onboard accelerometer. Data in the form of UIAcceleration objects can then be read via the UIAccelerometer.Acceleration event, which will fire every UIAccelerometer.UpdateInterval seconds.

In the following example, taken from the "GLPaintGameView" sample, the UIAccelerometer.UpdateInterval is set to 1/25th of a second and the OnAcceleration method is assigned to handle acceleration events. In the handler, the UIAccelerometerEventArgs parameter is used to determine the specifics of acceleration:

C# Example

      //Configure and enable the accelerometer
			const int AccelerometerFrequency = 25;
			UIAccelerometer.SharedAccelerometer.UpdateInterval = 1.0f / AccelerometerFrequency;
			UIAccelerometer.SharedAccelerometer.Acceleration += OnAccelerated;
          
    private void OnAccelerated (object sender, UIAccelerometerEventArgs e)
		{
#if LINQ
			myAccelerometer = new[]{e.Acceleration.X, e.Acceleration.Y, e.Acceleration.Z}
				.Select((v, i) => v * FilteringFactor + myAccelerometer [i] * (1.0f - FilteringFactor))
				.ToArray ();
#else
			myAccelerometer [0] = e.Acceleration.X * FilteringFactor + myAccelerometer [0] * (1.0 - FilteringFactor);
			myAccelerometer [1] = e.Acceleration.Y * FilteringFactor + myAccelerometer [1] * (1.0 - FilteringFactor);
			myAccelerometer [2] = e.Acceleration.Z * FilteringFactor + myAccelerometer [2] * (1.0 - FilteringFactor);
#endif
          ... etc ...
     }

Related content

Requirements

Namespace: MonoTouch.UIKit
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0

The members of MonoTouch.UIKit.UIAccelerometer are listed below.

See Also: NSObject

Public Constructors

Default constructor that initializes a new instance of this class with no parameters.
A constructor that initializes the object from the data stored in the unarchiver object.
Constructor to call on derived classes to skip initialization and merely allocate the object.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Public Properties

[read-only]
override
ClassHandleIntPtr. The handle for this class.
DelegateUIAccelerometerDelegate. An instance of the MonoTouch.UIKit.UIAccelerometerDelegate model class which acts as the class delegate.
[read-only]
static
SharedAccelerometerUIAccelerometer. The singleton instance of the UIAccelerometer.
UpdateIntervalDouble. The inerval, in seconds, between acceleration data events.|The interval, in seconds, between acceleration data events. Subject to hardwar
WeakDelegateNSObject. An object that can respond to the delegate protocol for this type

Protected Methods

override
Dispose(Boolean)
Releases the resources used by the UIAccelerometer object.

Public Events

AccelerationThis event is raised when a new acceleration event is ready.