O'Reilly logo

Beginning iOS 5 Application Development by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required