Using the shake gesture with the touch interface

In this recipe we will learn how to detect and handle iOS device motion events.

Getting ready

Create a new Single View Application, and name it ShakeExample.

How to do it...

To begin, follow these simple steps as outlined:

  1. Open the ViewController.m implementation file from the Project Navigator.
  2. Next, modify the viewDidLoad method as shown in the following code snippet:
    - (void)viewDidLoad{
      [super viewDidLoad];
      [self becomeFirstResponder];
      self.view.backgroundColor =[UIColor greenColor];
    }
  3. Next, create the following code sections as specified in the code snippet:
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) ...

Get Xcode 4 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.