Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Make moving shapes smoother (no need to select it first)
  • Loading branch information
ole committed Jan 30, 2012
1 parent ad9826d commit e07a679
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion PathHitTesting/Shape.m
Expand Up @@ -92,7 +92,7 @@ - (UIBezierPath *)tapTargetForPath:(UIBezierPath *)path
return nil;
}

CGPathRef tapTargetPath = CGPathCreateCopyByStrokingPath(path.CGPath, NULL, fmaxf(25.0f, path.lineWidth), path.lineCapStyle, path.lineJoinStyle, path.miterLimit);
CGPathRef tapTargetPath = CGPathCreateCopyByStrokingPath(path.CGPath, NULL, fmaxf(35.0f, path.lineWidth), path.lineCapStyle, path.lineJoinStyle, path.miterLimit);

if (tapTargetPath == NULL) {
return nil;
Expand Down
44 changes: 31 additions & 13 deletions PathHitTesting/ViewController.m
Expand Up @@ -17,6 +17,7 @@ @interface ViewController ()
@property (nonatomic, strong) Shape *selectedShape;

- (void)addShape:(Shape *)newShape;
- (Shape *)hitTest:(CGPoint)point;

@end

Expand Down Expand Up @@ -102,29 +103,46 @@ - (void)addShape:(Shape *)newShape
}


#pragma mark - Touch handling
#pragma mark - Hit Testing

- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
- (Shape *)hitTest:(CGPoint)point
{
CGPoint tapLocation = [tapRecognizer locationInView:self.drawingView];
__block Shape *hitShape = nil;
[self.shapes enumerateObjectsUsingBlock:^(id shape, NSUInteger idx, BOOL *stop) {
if ([shape containsPoint:tapLocation]) {
Shape *hitShape = [self.shapes objectAtIndex:idx];
self.selectedShape = hitShape;
NSLog(@"Hit shape: %@", hitShape);
if ([shape containsPoint:point]) {
hitShape = [self.shapes objectAtIndex:idx];
*stop = YES;
} else {
self.selectedShape = nil;
}
}];
return hitShape;
}


#pragma mark - Touch handling

- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint tapLocation = [tapRecognizer locationInView:self.drawingView];
self.selectedShape = [self hitTest:tapLocation];
}

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.drawingView];
[self.selectedShape moveBy:translation];
[self.drawingView setNeedsDisplay];
[panRecognizer setTranslation:CGPointZero inView:self.drawingView];
switch (panRecognizer.state) {
case UIGestureRecognizerStateBegan: {
CGPoint tapLocation = [panRecognizer locationInView:self.drawingView];
self.selectedShape = [self hitTest:tapLocation];
break;
}
case UIGestureRecognizerStateChanged: {
CGPoint translation = [panRecognizer translationInView:self.drawingView];
[self.selectedShape moveBy:translation];
[self.drawingView setNeedsDisplay];
[panRecognizer setTranslation:CGPointZero inView:self.drawingView];
}
default:
break;
}
}

@end

0 comments on commit e07a679

Please sign in to comment.