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 gyroscope


In this recipe, we will learn how to use the device's built-in gyroscope.

Getting ready

Create a new project in Xamarin Studio and name it GyroscopeApp.

Note

The simulator does not support the gyroscope hardware. Also, only newer devices contain a gyroscope. If this app is executed on a device without a gyroscope or on the simulator, no error will occur but no data will be displayed.

How to do it...

Perform the following steps:

  1. Add two buttons and a label to the view the controller.

  2. Add the MonoTouch.CoreMotion namespace in the GyroscopeAppViewController.cs file.

  3. Enter the following private field in the class:

    private CMMotionManager motionManager;
  4. Implement the ViewDidLoad method with the following code:

    this.motionManager = new CMMotionManager();
    this.motionManager.GyroUpdateInterval = 1 / 10;
    this.btnStart.TouchUpInside += delegate {
      this.motionManager.StartGyroUpdates(NSOperationQueue.MainQueue, this.GyroData_Received);
    } ;
    this.btnStop.TouchUpInside += delegate {
      this.motionManager...