Chapter 10. Gesture Recognizers

10.0. Introduction

Gestures are a combination of touch events. An example of a gesture can be found in the default iOS Photo application, which allows the user to zoom into and out of a photo while “pinching” the photo in and out using two fingers. Some of the most common gesture event detection code is encapsulated into reusable classes built into the iOS SDK. These classes can be used to detect swipe, pinch, pan, tap, drag, long-press, and rotation gestures.

Gesture recognizers must be added to instances of the UIView class. A single view can have more than one gesture recognizer. When a view catches the gesture, that view will be responsible for passing down the same gesture to other views in the hierarchy, if needed.

Some touch events required by an application might be complicated to process and might require the same event to be detectable in other views in the same application. This introduces the requirements for reusable gesture recognizers. There are six gesture recognizers in iOS SDK 5 and above:

  • Swipe

  • Rotation

  • Pinch

  • Pan

  • Long-press

  • Tap

The basic framework for handling a gesture through a built-in gesture recognizer is as follows:

  1. Create an object of the right data type for the gesture recognizer you want.

  2. Add this object as a gesture recognizer to the view that will receive the gesture.

  3. Write a method that is called when the gesture occurs and that takes the action you want.

The method associated as the target method of any gesture recognizer must follow ...

Get iOS 8 Swift Programming Cookbook 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.