Skip to content

Instantly share code, notes, and snippets.

@douglashill
Created November 18, 2013 11:53
Show Gist options
  • Save douglashill/7526596 to your computer and use it in GitHub Desktop.
Save douglashill/7526596 to your computer and use it in GitHub Desktop.
For detecting when an iOS device is shaken when the responder chain isn’t working for you. Handy for debugging. Subclass UIApplication, and pass in the new class name in UIApplicationMain().
NSString * const ShakeEventNotification = @"ShakeEventNotification";
@implementation ShakeApplication
{
BOOL _shakeStarted;
}
- (void)sendEvent:(UIEvent *)event
{
[super sendEvent: event];
if ([event subtype] == UIEventSubtypeMotionShake) {
// Shake events come in pairs. Only respond to the first.
if (_shakeStarted) {
_shakeStarted = NO;
} else {
_shakeStarted = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:ShakeEventNotification object:self];
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment