Created
November 22, 2012 06:27
-
-
Save weekwood/4129679 to your computer and use it in GitHub Desktop.
objective-c Shaking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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