19
UIGestureRecognizer and UIMenuController

In Chapter 18, you handled raw touches by implementing methods from UIResponder. Sometimes you want to detect a specific pattern of touches that make a gesture, like a pinch or a swipe. Instead of writing code to detect common gestures yourself, you can use instances of UIGestureRecognizer.

A UIGestureRecognizer intercepts touches that are on their way to being handled by a view. When it recognizes a particular gesture, it calls a method on the object of your choice. There are several types of gesture recognizers built into the SDK. In this chapter, you will use three of them to allow TouchTracker users to select, move, and delete lines (Figure 19.1). You will also see how to use another interesting iOS class, UIMenuController.

Figure 19.1  TouchTracker by the end of the chapter

Screenshot shows a detail view controller that has two random lines that are drawn, with one of them colored. A delete option is present near one of the lines.

UIGestureRecognizer Subclasses

You do not instantiate UIGestureRecognizer itself. Instead, there are a number of subclasses of UIGestureRecognizer, and each one is responsible for recognizing a particular gesture.

To use an instance of a UIGestureRecognizer subclass, you give it a target-action pair and attach it to a view. Whenever the gesture recognizer recognizes its gesture on the view, it will send the action message to its target. All UIGestureRecognizer action messages have the same form:

func action(_ gestureRecognizer: UIGestureRecognizer) { }

When recognizing a gesture, the gesture recognizer intercepts the touches destined for the view (Figure 19.2). Thus, the typical UIResponder methods like touchesBegan(_:with:) may not be called on a view with gesture recognizers.

Figure 19.2  Gesture recognizers intercept touches

Illustration shows an object diagram with gesture recognizers.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset