O'Reilly logo

Learning Objective-C by Developing iPhone Games by Amy M. Booker, Joseph D. Walters

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 other mechanisms

iPhone can detect motion events whenever the user shakes, moves, or tilts their device. iPhone has built-in hardware called an accelerometer and a gyroscope, which detect these types of events and pass the information back to your code.

Shake

Let's start with the shake event. When you want to detect a shake, you will need to designate your object as the first responder. To become the first responder, you will need to add the following code to your object:

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void) viewDidAppear:(BOOL)animated
{
    [self becomeFirstResponder];
}

Once you have your responder set up, you need to add a method to handle the event. There are three motion methods that you can use: motionEnded:withEvent:

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