Skip to content

Instantly share code, notes, and snippets.

@weekwood
Created November 22, 2012 06:27
Show Gist options
  • Save weekwood/4129679 to your computer and use it in GitHub Desktop.
Save weekwood/4129679 to your computer and use it in GitHub Desktop.
objective-c Shaking
static BOOL AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) {
double
deltaX = fabs(last.x - current.x),
deltaY = fabs(last.y - current.y),
deltaZ = fabs(last.z - current.z);
return
(deltaX > threshold && deltaY > threshold) ||
(deltaX > threshold && deltaZ > threshold) ||
(deltaY > threshold && deltaZ > threshold)
;
}
- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
if (self.lastAcceleration) {
if (AccelerationIsShaking(self.lastAcceleration, acceleration, 0.9)) {
histeresisExcited = YES;
/* SHAKE DETECTED. DO HERE WHAT YOU WANT. */
NSLog(@"start");
} else if (histeresisExcited && !AccelerationIsShaking(self.lastAcceleration, acceleration, 0.03)) {
histeresisExcited = NO;
NSLog(@"end");
}
}
self.lastAcceleration = acceleration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment