How to detect and program around shaking for iphone

I am trying to implement the “tutorial” trick on this page, but I think something is missing. I copied his accelerometer function to the myAppViewController.m file and put several nslog there to see if it even gets into the function when I use the shake function of the simulators. Nothing is displayed in the debug console.

http://mithin.in/2009/07/28/detecting-a-shake-using-iphone-sdk

Can someone explain what I can lose? Or point me to a textbook?

I found this that looks promising, but I don’t know how to “put it in a UIView”, How to determine when someone shakes the iPhone?


EDIT - now here is my working code due to the accepted answer.

Here is my code to detect the shake gesture in the 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 "); } } 
+6
source share
2 answers

You should absolutely not listen to the UIAccelerometer directly with your own filtering to handle UIAccelerometer events. This is powerful work and should only be used by applications that require a high sample rate of the accelerometer. Instead, use the new motion events that have been added to UIEvent :

http://developer.apple.com/IPhone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/EventHandling/EventHandling.html#//apple_ref/doc/uid/TP40007072-CH9-SW24

Like touches, a movement event will be transmitted to the first responder, and then move along the chain of responders if the first responder does not respond. UIEvent will be of type UIEventTypeMotion and a subtype of UIEventSubtypeMotionShake .

+4
source

Here is my answer that works:

//MainViewController.m

 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shake) name:@"shake" object:nil]; if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) NSLog(@"motion Began"); } -(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shake) name:@"shake" object:nil]; if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) NSLog(@"motion Ended"); } -(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shake) name:@"shake" object:nil]; if(event.type == UIEventTypeMotion && event.subtype == UIEventSubtypeMotionShake) NSLog(@"motion Cancelled"); } -(void)viewDidLoad { [super viewDidLoad]; [self becomeFirstResponder]; } - (void)viewDidUnload { // Release any retained subviews of the main view. // eg self.myOutlet = nil; [self resignFirstResponder]; } 

I tested only with a simulator and it returns me:

 2010-06-22 12:40:48.799 Cocktails[14589:207] motion Began 2010-06-22 12:40:48.800 Cocktails[14589:207] motion Ended 

I hope this help because I lose 2 hours to do this work.

+6
source

All Articles