The base class for all other gesture recognizer classes.
- watchOS 3.0+
Gesture recognizers simplify the event-handling process by tracking touch events for you and calling your custom code when those events match a specific pattern. Gesture recognizers report the results of their tracking to an action method that you define.
You do not subclass
WKGesture or create instances of it directly. Instead, you add concrete gesture recognizer objects to your Watch app’s storyboard file and connect that gesture recognizer to your custom action method. At runtime, you use the methods of this class to get and set the state of your gesture recognizer. The concrete gesture recognizer classes are as follows:
Executing Code in Response to a Gesture
A gesture recognizer has an associated action method that it calls during the recognition process to report on its progress. You define the action method in your interface controller and connect it to the gesture recognizer in Interface Builder. Action methods must conform to one of the following signatures:
The gesture recognizer calls your action method whenever the value in the
state property changes in a significant way. All gesture recognizers start out in the possible state and move to other states as appropriate based on the type of gesture. Gesture recognizers do not call your action method for every state change. For information about when the action method is called, see the constant descriptions of the
watchOS supports two broad categories of gesture recognizers: continuous gesture recognizers and discrete gesture recognizer.
Continuous Gesture Recognizers
Continuous gesture recognizers—for example, pan or long touch recognizers—track the user’s gesture and call the action method multiple times during a single gesture. They typically call the action method once when the gesture begins, one or more times as the gesture progresses, and once when the gesture either ends or is canceled. In your action method, use the gesture recognizer’s
state property to perform appropriate tasks based on the current state. For example:
began. Alter the user interface’s appearance to indicate that a gesture has begun.
changed. Update the user interface based on the current touch location.
ended. Return to the normal user interface appearance, indicating that the gesture has ended. Keep any updates.
cancelled. Return to the normal user interface appearance, indicating that the gesture has ended. Discard any updates.
Discrete Gesture Recognizers
Discrete gesture recognizers—for example, tap or swipe recognizers—only trigger a single event as soon as the gesture is recognized. For example, discrete recognizers call their action method when they enter the recognized state. If they enter the failed state, they fail silently.
Interface Builder Attributes
Xcode lets you configure information about your gesture recognizer in your storyboard file. Table 1 lists the attributes you can configure in your storyboard and their meaning.
A checkbox indicating whether the gesture recognizer is enabled. You can also configure this value programmatically using the
Checkboxes indicating additional gesture recognizer behaviors. Use these options to specify how the gesture recognizer responds to touch events.
Must Fail First
Gesture recognizers that must fail before the current one can succeed. Drag from this option to the gesture recognizers in your storyboard file.