Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

1.9. Accelerometer

The Accelerometer is one of the interesting features of the iPhone. The Accelerometer allows a program to read when the iPhone is moving and return data about the movement of the device. The iPhone OS and MonoTouch provide a software solution to integrating with the Accelerometer and handling acceleration events. The SharedAccelerometer is a shared C# object that provides access to the acceleration hardware on the device. The following code shows how to handle acceleration events and to display that information to the user.

// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
// This method is invoked when the application has loaded its
// UI and is ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// If you have defined a view, add it here:
// window.AddSubview (navigationController.View);
window.MakeKeyAndVisible ();
//Acceleration does not work in the Simulator.
UIAccelerometer.SharedAccelerometer.UpdateInterval = 1 / 10;
dataLabel.Text = String.Empty;
UIAccelerometer.SharedAccelerometer.Acceleration += delegate(object sender,
UIAccelerometerEventArgs e) {
       UIAcceleration acc = e.Acceleration;
       double thresholdValue = 2.0;
       double Velocity = Math.Sqrt(Math.Pow(acc.X, 2) +
                                   Math.Pow(acc.Y, 2) +
                                   Math.Pow(acc.Z, 2));
       string strReturn = System.Environment.NewLine;
       if ( Velocity > thresholdValue ) {

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial