Discover when and how to build your own gesture recognizers
When the built-in UIKit gesture recognizers do not provide the behavior you want, you can define custom gesture recognizers. UIKit defines highly configurable gesture recognizers to handle touch sequences for taps, long presses, pans, swipes, rotations, and pinches. For other touch sequences, or to handle gestures that involve button presses, you can define a custom gesture recognizer.
You might also use a custom gesture recognizer to simplify the event-handling code in your app. For example, the Leveraging Touch Input for Drawing Apps sample uses a gesture recognizer to capture input and display it onscreen, as shown in Figure 1.
To define a custom gesture recognizer, subclass
UIGesture (or one of its subclasses). At the top of your source file, import the
UIGesture header file (for Objective-C) or the
UIKit module (for Swift), as shown in Listing 1. This header file defines the methods and properties that you must override to implement your custom gesture recognizer.
In your custom subclass, implement whatever methods you need to process events. For example, if your gesture consists of touch events, implement the
touches methods. Use incoming events to update the
state property of your gesture recognizer. UIKit uses the gesture recognizer states to coordinate interactions with other objects in your interface.