O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 10. Implementing 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. Once 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required