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
}
}
}
Written by Alex Hajdu
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Xcode
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#