I’m trying to get shake gesture to work. Here is some code
In my implementation file
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.type == UIEventSubtypeMotionShake)
{
NSLog(@"Shake gesture detected");
}
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
I read that in order to make shake gesture to work UIView should be the firstResponder. This is why I added that code in implementation
if(self.view.isFirstResponder)
{
NSLog(@"first");
}
else
{
NSLog(@"no");
}
- (void)viewDidAppear {
[self becomeFirstResponder];
}
When I run the app the output of NSLog is NO. What I miss ? And how to get shake gesture to work
I guess you’re doing this in your UIViewController? That’s not correct … You have to subclass UIView like this:
ShakeView.h:
ShakeView.m:
Then use ShakeView instead of your “normal” UIView and implement this in your UIViewController:
That’s it. Hope it helps 🙂