A UIGestureRecognizer Tutorial

Image for post
Image for post
Photo by Eugene Chystiakov on Unsplash

Prerequisites:

Terminology

UIGestureRecogniser: The base class for gesture recognisers, which include tap and pinch recognisers

The end result

This isn’t going to be pretty. You’re going to see some squares moving around an App according to whether the user drags, drops, pinches or rotates. Thomas Was Alone doesn’t have anything on this stuff!

The setup

The setup for this has been done directly in Playgrounds. In practice I’ve already written a tutorial for this, but to get this working I used import PlaygroundSupport at the top of the playground and set the live view with PlaygroundPage.current.liveView = MyViewController(). I’ve written the gestures in viewDidLoad() and kept things tidy by using loadView() (there is a guide on this HERE). Don’t worry though, this is really about the gestures and not the setup, and the full Playground is at the bottom of this article.

The theory

The UIGestureRecognizer class help us detect common gestures like taps, pinches, swipes, pans, long presses and rotations. This avoids having to register touch notifications for each UIView and then use touchesBegan(_:with:) for that view. Let us avoid that kerfuffle and get started!

A pan gesture

We set up the pan recogniser in viewDidLoad() with the following couple of lines to create the gesture recogniser .

Image for post
Image for post
Click for Gist
Image for post
Image for post
Click for Gist

Pinch

Option + Shift to pinch is the way to get those two circles on a view to enable to pinch gesture is the way to go there. It can be a little tricky in the Playground, but it does work in the end (if you cannot get it working then perhaps using the full simulator is the way to go).

Image for post
Image for post
Image for post
Image for post

A tap gesture

We might wish to register when the user taps an object on the screen.

Image for post
Image for post
Image for post
Image for post

A long-press gesture

We might wish to register when the user taps an object on the screen for a longer period than the simple tap gesture.

Image for post
Image for post
Image for post
Image for post

A rotation gesture

We might wish to rotate our square when a user selects it with two fingers.
The UIRotationGestureRecognizer looks for this rotation:

Image for post
Image for post
Image for post
Image for post

A swipe

I’d say that it makes more sense to apply a swipe gesture to the whole UIViewController rather than an object.

Image for post
Image for post
Image for post
Image for post

Multiple views per gesture

Unfortunately this is not possible. Because every gesture can only be attached to one view this is unfortunately rather a fixed rule.

Did you make sure the user can interact?

When you add a gesture to a UIImageView it is a common issue that isUserInteractionEnabled is set by default as false. So do this: Make sure isUserInteractionEnabled is set as true either in the Storyboard or though code.

Conclusion:

Pinches, taps and rotations are great ways for users to interact with iOS applications. Making your code a must for the disconcerting user.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store