Shaking It Up With iOS Shake Gestures

Posted by Grego on July 12, 2015

Today’s quick tip is about adding that super cool (or super annoying) iOS shake gesture. Now typically I’m not a big fan of the shake gesture, I feel like it’s a little difficult to trigger and I’m always afraid that I might send my phone flying straight into the Apple Store for a replacement. I also feel like if the shake gesture activates a feature I might want to use multiple times in rapid succession, it’s not a good user experience. Despite all of my bad criticism towards the shake gesture, I was working on a project that called for a random selection and the shake gesture also identified with the name of the app, so I decided to add it in as a novelty.

The shake gesture can be added in two quick steps:

  1. Make your VC the first responder (can be done via code)
  2. Implement the motionEnded:withEvent method.

In short:

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)motionEnded:(UIEventSubtype)motion
          withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake) {
        NSLog(@"Shake, shake, shake senora~");
    }
}

Happy shaking and try to avoid dropping your phones!