Book Image

iOS Development with Xamarin Cookbook

By : Dimitrios Tavlikos (USD)
Book Image

iOS Development with Xamarin Cookbook

By: Dimitrios Tavlikos (USD)

Overview of this book

Table of Contents (22 chapters)
iOS Development with Xamarin Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using the accelerometer


In this recipe, we will learn how to receive the accelerometer events to create an app that is aware of the device movement.

Getting ready

Create a new Single View Application in Xamarin Studio and name it AccelerometerApp.

Note

The simulator does not support the accelerometer hardware. The project in this example will work correctly on a device.

How to do it...

Perform the following steps:

  1. Add two buttons and a label on the view of the controller.

  2. In the ViewDidLoad method, add the following code:

    this.btnStop.Enabled = false;
    UIAccelerometer.SharedAccelerometer.UpdateInterval = 1 / 
      10;
    this.btnStart.TouchUpInside += delegate {
      this.btnStart.Enabled = false;
      UIAccelerometer.SharedAccelerometer.Acceleration += this.Acceleration_Received;
      this.btnStop.Enabled = true;
    } ;
    this.btnStop.TouchUpInside += delegate {
      this.btnStop.Enabled = false;
      UIAccelerometer.SharedAccelerometer.Acceleration -= this.Acceleration_Received;
      this.btnStart.Enabled = true;
    } ;
  3. Add the...