O'Reilly logo

Xcode 4 iOS Development 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

Time for action – implementing the motionBegan, motionEnded, and motionCancelled methods

We are now ready to start implementing the code that will be used to detect when a shake has occurred on the iOS device. We will learn about the various motion methods and how to implement these by following these simple steps:

  1. To begin, open the ShakeExampleViewController.m implementation file, located within the Classes folder of your project and add the following code:
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake)
    {
    self.view.backgroundColor=[UIColor yellowColor];
    NSLog(@"Device has been shaken");
    }
    }
    

    Next, we need to implement the code to handle ...

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