Created
June 19, 2010 21:14
-
-
Save reflog/445282 to your computer and use it in GitHub Desktop.
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
// 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