Skip to content

Instantly share code, notes, and snippets.

@reflog
Created June 19, 2010 21:14
Show Gist options
  • Save reflog/445282 to your computer and use it in GitHub Desktop.
Save reflog/445282 to your computer and use it in GitHub Desktop.
// Inherit your main window from this custom class:
@implementation MotionWindow
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) {
NSLog(@"Global Shake!");
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeviceShaken" object:self];
}
}
@end
// Then in any view controller:
- (void)deviceShaken {
if(inShake) {
// NSLog(@"in shake. skip");
return;
}
NSTimeInterval newTimeOfShake = [[NSDate date] timeIntervalSince1970];
if(newTimeOfShake - timeOfShake < 1 && timeOfShake != 0){
// NSLog(@"too soon shake. skip");
return;
}
timeOfShake = newTimeOfShake;
// ============== add your code here ==========
inShake = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceShaken) name:@"DeviceShaken" object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment