Book Image

Xcode 4 Cookbook

By : Steven F. Daniel
Book Image

Xcode 4 Cookbook

By: Steven F. Daniel

Overview of this book

<p>The release of iOS 6 comes packed with over 1,500 new APIs and 200 new features. Xcode 4 Cookbook will teach youhow to integrate iCloud storage and how to go about using the Facebook and OpenGraphi APIs, as well as providing you with practical step-by-step recipes covering User Interfaces, data management, multimedia, localisation services and maps, right through to application deployment to the Apple App Store. You will soon be mastering the technology and the skills needed to create some amazing applications.<br /><br />"Xcode 4 Cookbook" will help you learn how to build some powerful applications using iOS 6 and the various frameworks. You will soon master how to incorporate iCloud, Facebook, and the OpenGraph APIs and apply various image filters and transitions using Core Image integration within your applications. By using the book’s step-by-step approach, you will soon master the technology and the skills needed to create some amazing applications.<br /><br />"Xcode 4 Cookbook" provides you with the skills and knowledge and practical recipes on how to go about developing useful applications that can be used within the industry.<br /><br />You will start by learning how to go about downloading and installing the Xcode Development Tools, learn about Interface Builder to create the visual user interfaces, connecting the objects using Outlets and Actions, and learn how to compile/debug your applications.<br /><br />Finally, you will learn how to capture media with the iOS camera and play back video content using Airplay to wirelessly stream videos to an Apple TV device, using the AV Foundation framework, as well as using the Core Image and Core Graphics frameworks to create amazing image effects using the built-in features.</p>
Table of Contents (19 chapters)
Xcode 4 Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Working with the iOS device gyroscope


In this recipe we will learn how to use the features of the Gyroscope events of the iOS device.

Getting ready

We have looked at how to use the iOS devices accelerometer and modify the views background color based on the orientation of the axes. In this recipe we will look at how to incorporate the gyroscope features.

How to do it...

To begin, follow these simple steps as outlined:

  1. Open the AccelGyroExample.xcodeproj project file.

  2. Open the ViewController.m implementation file and modify the viewDidLoad method as shown in the following highlighted code snippet:

    - (void)viewDidLoad
    {
      [super viewDidLoad];
      // Set up the accelerometer
      UIAccelerometer *accelerometer = [UIAccelerometer
      sharedAccelerometer];
      accelerometer.updateInterval = 0.5;
      accelerometer.delegate = self;
      // Perform a check to see if the device 
      // supports the Gyroscope feature
      if ([self isGyroscopeAvailable] == YES) {
        motionManager = [[CMMotionManager alloc] init];
        [motionManager
        startGyroUpdatesToQueue:[NSOperationQueue
        currentQueue]
        withHandler:^(CMGyroData *gyroData, NSError *error)
        {
          [self doGyroRotation:gyroData.rotationRate];
        }];
      }
      else {
        // Device does not support the gyroscope feature
        NSLog(@"No Gyroscope detected.");
      }
    }

    Next, create the following code sections, as specified in the following code snippet:

    // Handles rotation of the Gyroscope
    - (void)doGyroRotation:(CMRotationRate)rotation {
      double value = 
      (fabs(rotation.x)+fabs(rotation.y) + fabs(rotation.z)) / 8.0;
      if (value > 1.0) { value = 1.0;}
      self.view.alpha = value;
    }
    // Checks to see if Gyroscope is available on the device
    - (BOOL) isGyroscopeAvailable
    {
      #ifdef __IPHONE_4_0
      CMMotionManager *gyroManager = 
      [[CMMotionManager alloc] init];
      gyroManager.gyroUpdateInterval = 1.0 / 60.0;
      BOOL gyroAvailable = gyroManager.gyroAvailable;
      return gyroAvailable;
      #else
      return NO;
      #endif
    }
  3. Change the deployment target to your iOS device.

  4. Build and run the application by choosing Product | Run from the Product menu or alternatively by pressing Command + R.

When the compilation completes, the application will be displayed onto your iOS device. Try moving your device in all directions to see the background color start cycling through the various colors.

How it works…

What we have done in this recipe is added code to determine by using the #Ifdef __IPHONE_4_0 directive if the device currently in use is an iPhone 4. If this is the case, it then checks to see if the device supports the gyroscope feature and a Boolean status YES is returned; otherwise NO is returned.

Next, we set up our UIAccelerometer delegate and update intervals to be twice per second in order to request updates. We then make a call to our isGyroscopeAvailable function to check to see if the gyroscope feature is supported.

Finally, we call the startGyroUpdatesToQueue function and add a handler to call our doGryroRotation function that then updates the alpha blend color of our view. If no gyroscope feature is supported, this is logged out to the debug window.

Note

To start receiving and handling rotation-rate data for the gyroscope feature, you need to create an instance of the CMMotionManager class and call one of the following methods to it.

The following image shows how the iPhone responds to changes on its three axes when the iPhone is tilted. Under normal gravity, each of these values will be between -1 and +1 with a value of 0 being the middle center point. Moving the phone in a rapid motion will increase these values:

The following table explains each of the method calls relating to the CMMotionManager class:

CMMotionManager methods

Description

startGyroUpdates

When this method is called, Core Motion kicks in and continuously updates the gyroData property of the CMMotionManager class with the latest measurement of activity.

startGyroUpdatesToQueue:withHandler

Before calling this method, you need to ensure that you have set the update interval of the gyroUpdateInterval property.

When this method is called, it creates an NSOperationQueue event that queues the gyroscope event that then fires when the update interval has been reached, then calls the function and passes it the latest gyroscope data.

stopGyroUpdates

This method turns off the Core Motion sensors and stops all updates of motion data. It is a good idea to always stop gyro updates as this will save battery power.

Note

If you would like to find out more information about the CoreMotion class, you can refer to the Apple Developer documentation located on the link http://developer.apple.com/library/ios/#documentation/CoreMotion/Reference/CoreMotion_Reference/_index.html.

See also

  • The Using the shake gesture with the touch interface recipe

  • The Sensing movement with the accelerometer input recipe

  • The Using Xcode to create an iOS project recipe in Chapter 1, Getting and Installing the iOS SDK Development Tools