Sometimes you may want to detect shake gesture in iPhone using your programs. But how can you detect shake gesture in iPhone? Shake gesture in iPhone can be detected by using the accelerometer. The following lines of code can be used for detecting the shake gesture in iPhone. You can use these lines of code in your cocos2D applications/games.

First of all you MUST enable accelerometer in the init function

isAccelerometerEnabled = YES;

Then use the following function to detect the shake gesture.

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
    if(abs(acceleration.x-oldX )>0.7 || abs(acceleration.y-oldY)>0.7)
       NSLog(@”Yupie! Shake detected!”);
oldX = acceleration.x;
oldY = acceleration.y;

Here oldX & oldY are variables that store accelerometer values and 0.7 is the value you are giving to check for shake gesture. That’s it. 🙂



  1. chips zynga June 18, 2011 at 8:56 pm

    Greetings! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your posts. Can you suggest any other blogs/websites/forums that go over the same topics? Many thanks!

  2. elektro-fahrrad motor June 20, 2011 at 2:39 pm

    Ang blog na ito ay malaki. Ako ay lubos na sigurado na ang mga tao ay mahanap ito dahil interesante ako tiyak.


Leave a Reply

Your email address will not be published. Required fields are marked *




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.