I'm trying to implement the shake "tutorial" on this page, but I think I'm missing something. I copied his accelerometer function into myAppViewController.m file and put some nslogs in there to see if it even gets into the function when I use the simulators "shake" function. Nothing shows up in the debug console.
http://mithin.in/2009/07/28/detecting-a-shake-using-iphone-sdk
Can anyone explain what I might be missing? Or point me to a tutorial?
I found this, which looks promising, but I don't know how to "put it in a UIView" How do I detect when someone shakes an iPhone?
EDIT - now here's my working code because of the accepted answer's suggestion.
Here's my code to detect the shake gesture in 3.0 iphone sdk.
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"{motion ended event ");
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"{shaken state ");
}
else {
NSLog(@"{not shaken state ");
}
}
becomeFirstResponder
is unnecessary. The only action that is required to allow your viewController to respond to motion events is to ensure that the viewController can become firstResponder. As described in the doco, UIView passesmotionBegan
etc. messages up the responder chain, which will eventually find the view's controller, so long ascanBecomeFirstResponder
returns YES. – Addam