O'Reilly logo

Xcode 4 Cookbook by Steven F. Daniel

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

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