PDA

View Full Version : Cant figure out how to detect a Shake Motion **Help**




iJustinCabral
Aug 21, 2012, 05:28 PM
My code right now is



-(void)viewWillAppear:(BOOL)animated
{

[super viewWillAppear:animated];

[self becomeFirstResponder];

}


-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

if (motion == UIEventSubtypeMotionShake)
{
[self.theList removeAllObjects];
NSLog (@"You shook the device. New List Count %d",self.theList.count);
}

}

-(void)motionEnded:(UIEventSubtype)motion withEvent(UIEvent *)event
{
NSLog(@"Device finished shaking");
}



Both NSLogs are not appearing in the debugger, so the device isn't getting this method sent to it.

I thought i did everything right but the device still wont recognize the shake...any idea what I'm missing or doing wrong?



Reason077
Aug 21, 2012, 06:14 PM
The object where you implemented these methods needs to be the "first responder" in order for UIEvents to be delivered to it.

Make sure you've implemented canBecomeFirstResponder to return YES, and are calling becomeFirstResponder at some point.

iJustinCabral
Aug 21, 2012, 09:14 PM
Implemented

-(BOOL)canBecomeFirstResponder
{
return YES;
}

with the rest of my code and it worked perfect. Wonder why I missed that in the documentation.

Thanks again reason!