Last Updated: February 25, 2016
·
2.808K
· alexhajdu

How to make gentle shaking gesture

Today I implemented shake gesture in a game. I noticed I must to shake with my iPad really hard to make things happened. (default setting).

Here is simple solution for making shaking more gentle. Just experiment with accelerationTreshold value.

.h

@interface ViewController : UIViewController <UIAccelerometerDelegate>    

.m

// customize here
#define accelerationThreshold    1.3

- (void)viewDidLoad
{
    UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
    accel.delegate = self;
    accel.updateInterval = 1.0f/60.0f;
}

// event handling
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
  if (fabsf(acceleration.x) > accelerationThreshold || fabsf(acceleration.y) > accelerationThreshold || fabsf(acceleration.z) > accelerationThreshold) {
      // your action here  
  }
}
}