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).


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

First, there's a standard ...

Get Learning Xcode 8 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.