Graphics Programs Reference
In-Depth Information
UIPanGestureRecognizer and Simultaneous Recognizers
Once a line is selected during a long press, we want the user to be able to move that line
around the screen by dragging it with a finger. So we need a gesture recognizer for a finger
moving around the screen. This gesture is called
panning
, and its gesture recognizer sub-
class is
UIPanGestureRecognizer
.
Normally, a gesture recognizer does not share the touches it intercepts. Once it has recog-
nized its gesture, it “eats” that touch, and no other recognizer gets a chance to handle it. In
our case, this is bad: the entire pan gesture we want to recognize happens within a long
press gesture. We need the long press recognizer and the pan recognizer to be able to recog-
nize their gestures simultaneously. Let's see how to do that.
First, in
TouchDrawView.h
, declare that
TouchDrawView
conforms to the
UIGes-
tureRecognizerDelegate
protocol and declare a
UIPanGestureRecognizer
as an instance variable.
@interface TouchDrawView : UIView
<UIGestureRecognizerDelegate>
{
NSMutableDictionary *linesInProcess;
NSMutableArray *completeLines;
UIPanGestureRecognizer *moveRecognizer;
}
In
TouchDrawView.m
, add code to
initWithFrame:
to instantiate a
UIPanGes-
tureRecognizer
, set two of its properties, and attach it to the
TouchDrawView
.
[self addGestureRecognizer:pressRecognizer];
moveRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(moveLine:)];
[moveRecognizer setDelegate:self];
[moveRecognizer setCancelsTouchesInView:NO];
[self addGestureRecognizer:moveRecognizer];
There are a number of methods in the
UIGestureRecognizerDelegate
protocol,
but we are only interested in one -
gestureRecog-
nizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
. A
gesture recognizer will send this message to its
delegate
when it recognizes its gesture
but realizes that another gesture recognizer has recognized its gesture, too. If this method
returns
YES
, the recognizer will share its touches with other gesture recognizers.