Graphics Programs Reference
In-Depth Information
UILongPressGestureRecognizer
Let's test out two other subclasses of UIGestureRecognizer : UILongPressGes-
tureRecognizer and UIPanGestureRecognizer . When the user holds down on a
line (a long press), that line should be selected. While a line is selected in this way, the user
should be able to drag the line (a pan) to a new position.
In this section, we'll focus on the long press recognizer. In TouchDrawView.m , instanti-
ate a UILongPressGestureRecognizer in initWithFrame: and add it to the
TouchDrawView .
[self addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *pressRecognizer =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(longPress:)];
[self addGestureRecognizer:pressRecognizer];
Now when the user holds down on the TouchDrawView , the message longPress:
will be sent to it. By default, a touch must be held 0.5 seconds to become a long press, but
you can change the minimumPressDuration of the gesture recognizer if you like.
A tap is a simple gesture. By the time it is recognized, the gesture is over, and a tap has oc-
curred. A long press, on the other hand, is a gesture that occurs over time and is defined by
three separate events.
For example, when the user touches a view, the long press recognizer notices a possible
long press but must wait and see whether this touch is held long enough to become a long
press gesture.
Once the user holds the touch long enough, the long press is recognized and the gesture has
begun . When the user removes the finger, the gesture has ended .
Each of these events causes a change in the gesture recognizer's state property. For in-
stance, the state of the long press recognizer described above would be UIGes-
tureRecognizerStatePossible , then UIGestureRecognizerStateBegan ,
and finally UIGestureRecognizerStateEnded .
When a gesture recognizer transitions to any state other than the possible state, it sends its
action message to its target. This means the long press recognizer's target receives the same
message when a long press begins and when it ends. The gesture recognizer's state allows
Search WWH ::




Custom Search