Monday, July 4, 2011

Shake Gesture

In this post we will see how to work with the device when it is shaken
Design Phase: As you can see from the image given below I have used the picker view to perform some shake operations, when you launch the simulator from and select the hardware menu their you will see a option called as shake the device with the help of which we can perform the testing for those application which require shaking of the device.

Note: For the shake event to work it is clearly mentioned in the documentation that the
iOS informs the first responder only when a motion event starts and when it ends; for example, it doesn’t report individual shakes. The receiving object must be the first responder to receive motion events.
The UIResponder class is responsible for handling the touches events and tapcounts into your app as well as the motions and has the corresponding events to handle the same, so you may have a look at the UIResponder class documentation which explains it all in a neat fashion.

Step 1: Open Xcode create a windows based application and give it an appropriate name and then add the UIViewController subclass file with the name myViewController this will add two files into your application with the name myViewController.h and myViewController.m

Step 2: Create the picker view and if you need any reference in creating one please refer this post of mine.

Step 3: As its said in the docs that in order to receive the motion event the receiving object must be the first responder so we will have to make the UIViewController subclass as the first responder so that it can receive the motion events so here's how we are going to do that

    [super viewDidAppear:YES];
    [self becomeFirstResponder];

- (BOOL)canBecomeFirstResponder
    return YES;

Step 4: The app works like this when I will press the shake menu from the hardware section the I will call a function which generates random number and will supply this random number to the picker method called as 

 selectRow:<#(NSInteger)#> inComponent:<#(NSInteger)#> animated:<#(BOOL)#>

Here’s a view at the function that generates the random number

    int r = arc4random() %[teamName count];
     [_pickerView selectRow:r inComponent:0 animated:YES];

And here’s the function which responds to the motion event that you may copy paste from the UIResponder class reference

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event


    if(event.type == UIEventSubtypeMotionShake)



        [self SelectPickerRow];




Code Explanation: In the above code I am checking if the device has been shaken or not, and if it is then I am calling the SelectPickerRow function which will play its part is selecting a row from the picker with the help of a random number that is generated.

Step 5: Select the appdelegate.m file and add this view to the iPhone window here’s the code to do that

#import "ShakeDemoAppDelegate.h"
#import "myViewController.h"

@implementation ShakeDemoAppDelegate

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    // Override point for customization after application launch.
    myViewController *obj = [[myViewController alloc]init];
    [self.window addSubview:obj.view];
    [self.window makeKeyAndVisible];
    return YES;


When you run this application you will see the application running inside the simulator, look at the menu bar it will say iPhone simulator now and from their select the Hardware menu and select the Shake submenu from their you will see that the device is shook and the selection indicator will point towards a new value from the picker.

You may deploy this application into the device and then run the application on the device or you may test this app inside the simulator as well.

I hope this post has helped you in helping you in understanding how to work with motions, if you have any queries or suggestions then please put them in the form of comment or you may even mail me your code at my mail id which is given in the facebook badge and I will surely reply to that also if you want some tutorial on this blog then please request me a tutorial at this post. Until then happy iCoding and have a great Day.

1 comment:

  1. Thanks for this tutorial, it is just what I'm after.
    I want to create an app which randomly selects from a 3 component pickerView.
    I'm just starting coding so I need a little extra help.
    I don't know how or where to put the -- selectRow:<#(NSInteger)#> inComponent:<#(NSInteger)#> animated:<#(BOOL)#> ---
    Does the random generation supply the info for the expressions?

    Xcode is also saying that [ self SelectPickerRow]; "No visible @interface for 'ViewController' declares the selector 'SelectPickerRow'

    And -(void)SelectPickerRow "Use of undeclared identifier 'SelectPickerRow"

    Could you help please?
    Also how do I apply it to 3 components?

    Thanks, Karl
    Xcode 4.6.3