USING THE SHAKE API TO DETECT SHAKES

Beginning with the iPhone OS 3, Apple introduced the Shake API, which helps your application to detect shakes to the device. In reality, this API comes in the form of three events that you can handle in your code:

  • motionBegan:
  • motionEnded:
  • motionCancelled:

These three events are defined in the UIResponder class, which is the superclass of UIApplication, UIView, and its subclasses (including UIWindow). The following Try It Out shows you how to detect shakes to your device using these three events.

TRY IT OUT: Using the Shake API

image

  1. Using Xcode, create a new Single View Application (iPhone) project and name it Shake. You will also use the project name as the Class Prefix and ensure that you have the Use Automatic Reference Counting option unchecked.
  2. Select the ShakeViewController.xib file to edit it in Interface Builder.
  3. Populate the View window with the following views (the result will look like Figure 15-10):
    • TextField
    • DatePicker

    image

    FIGURE 15-10

  4. Insert the following statements that appear in bold into the ShakeViewController.h file:
    #import <UIKit/UIKit.h>
    
    @interface ShakeViewController : UIViewController
    {
        IBOutlet UITextField *textField;
        IBOutlet UIDatePicker *datePicker;
    }
    
    @property (nonatomic, retain) UITextField *textField;
    @property (nonatomic, ...

Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.