Graphics Programs Reference
In-Depth Information
- (void)tap:(UIGestureRecognizer *)gr
{
NSLog(@"Recognized tap");
// If we just tapped, remove all lines in process
// so that a tap doesn't result in a new line
[linesInProcess removeAllObjects];
[self setNeedsDisplay];
}
Build and run again. Now a tap should have no effect except for logging a statement to the
console.
Now, in order to “select a line,” we need to find a line close to where the tap occurred and
then store a reference to that line for later. In TouchDrawView.h , declare a new meth-
od and a new property. Also, at the top of this file, forward declare the Line class.
@class Line;
@interface TouchDrawView : UIView
{
NSMutableDictionary *linesInProcess;
NSMutableArray *completeLines;
}
@property (nonatomic, weak) Line *selectedLine;
- (Line *)lineAtPoint:(CGPoint)p;
- (void)clearAll;
- (void)endTouches:(NSSet *)touches;
@end
Synthesize selectedLine in TouchDrawView.m .
@implementation TouchDrawView
@synthesize selectedLine;
Implement lineAtPoint: in TouchDrawView.m to get a Line close to the given
point.
- (Line *)lineAtPoint:(CGPoint)p
{
// Find a line close to p
for (Line *l in completeLines) {
CGPoint start = [l begin];
CGPoint end = [l end];
// Check a few points on the line
for (float t = 0.0; t <= 1.0; t += 0.05) {
float x = start.x + t * (end.x - start.x);
float y = start.y + t * (end.y - start.y);
// If the tapped point is within 20 points, let's return this line
if (hypot(x - p.x, y - p.y) < 20.0) {
return l;
Search WWH ::




Custom Search