DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Robert has posted 10 posts at DZone. View Full User Profile

Detect A Shake

10.23.2008
| 253 views |
  • submit to reddit
        // Detecting a shake


// setup code
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:( 1.0 / 40 )];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{
   const float violence = 1.5;
   static bool isProcessing;
   bool isShake = FALSE;
	
   if ( isProcessing )
   {
      return;
   }
	
   isProcessing = TRUE;
   
   if ( acceleration.x > violence * 1.5 || acceleration.x < ( -1.5 * violence ) )
   {
      isShake = TRUE;
   }
   if ( acceleration.y > violence * 2 || acceleration.y < ( -2 * violence ) )
   {
      isShake = TRUE;
   }
   if ( acceleration.z > violence * 3 || acceleration.z < ( -3 * violence ) )
   {
      isShake = TRUE;
   }
   if ( isShake ) 
   {
      // YOUR CODE HERE 
   } 
   isProcessing = FALSE;
}