Adding gestures from code

While implementing gestures from the storyboard is a simple and visual way to set up gestures, sometimes you'll need to get into the details and create them purely with code. In this section, we're going to look at how to do just that, by adding a pinch gesture recognizer that allows us to scale our image up and down.

Creating a gesture through code

The first thing we're going to need to do is create a property to hold our gesture. Since the gesture is going to be created outside of initialization, we are going to have to make it an implicitly unwrapped optional value (remember, that is shown with the exclamation mark after the variable name).

Note

Here's a quick refresher on optional values in Swift:

First, there's a standard ...

Get Learning Xcode 8 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.