Graphics Programs Reference
In-Depth Information
return;
// When the pan recognizer changes its position...
if ([gr state] == UIGestureRecognizerStateChanged) {
// How far as the pan moved?
CGPoint translation = [gr translationInView:self];
// Add the translation to the current begin and end points of the line
CGPoint begin = [[self selectedLine] begin];
CGPoint end = [[self selectedLine] end];
begin.x += translation.x;
begin.y += translation.y;
end.x += translation.x;
end.y += translation.y;
// Set the new beginning and end points of the line
[[self selectedLine] setBegin:begin];
[[self selectedLine] setEnd:end];
// Redraw the screen
[self setNeedsDisplay];
}
}
Build and run the application. Touch and hold on a line and begin dragging - and you'll
immediately notice that the line and your finger are way out of sync. This makes sense be-
cause you are adding the current translation over and over again to the line's original end
points. We really need the gesture recognizer to report the translation since the last time
this method was called instead. Fortunately, we can do this. You can set the translation of
a pan gesture recognizer back to the zero point every time it reports a change. Then, the
next time it reports a change, it will have the translation since the last event.
Near the bottom of moveLine: in TouchDrawView.m , add the following line of
code.
[self setNeedsDisplay];
[gr setTranslation:CGPointZero inView:self];
}
}
Build and run the application and move a line around. Works great!
Before moving on, let's take a look at a property you set in the pan gesture recognizer -
cancelsTouchesInView . Every UIGestureRecognizer has this property and,
by default, this property is YES . This means that the gesture recognizer will eat any touch
it recognizes so that the view will not have a chance to handle it via the traditional
UIResponder methods, like touchesBegan:withEvent: .
Usually, this is what you want, but not always. In our case, the gesture that the pan recog-
nizer recognizes is the same kind of touch that the view handles to draw lines using the
Search WWH ::




Custom Search