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

Commit

Permalink
Highlight selected shape
Browse files Browse the repository at this point in the history
  • Loading branch information
ole committed Feb 1, 2012
1 parent 418d4fd commit 528223d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions PathHitTesting/DrawingView.h
Expand Up @@ -27,4 +27,7 @@
- (UIBezierPath *)drawingView:(DrawingView *)drawingView pathForShapeAtIndex:(NSUInteger)shapeIndex;
- (UIColor *)drawingView:(DrawingView *)drawingView lineColorForShapeAtIndex:(NSUInteger)shapeIndex;

@optional
- (NSUInteger)indexOfSelectedShapeInDrawingView:(DrawingView *)drawingView;

@end
15 changes: 14 additions & 1 deletion PathHitTesting/DrawingView.m
Expand Up @@ -27,13 +27,26 @@ - (void)reloadDataInRect:(CGRect)rect
- (void)drawRect:(CGRect)rect
{
NSUInteger numberOfShapes = [self.dataSource numberOfShapesInDrawingView:self];
NSUInteger indexOfSelectedShape = NSNotFound;
if ([self.dataSource respondsToSelector:@selector(indexOfSelectedShapeInDrawingView:)]) {
indexOfSelectedShape = [self.dataSource indexOfSelectedShapeInDrawingView:self];
}

for (NSUInteger shapeIndex = 0; shapeIndex < numberOfShapes; shapeIndex++) {
UIBezierPath *path = [self.dataSource drawingView:self pathForShapeAtIndex:shapeIndex];
if (CGRectIntersectsRect(rect, CGRectInset(path.bounds, -path.lineWidth, -path.lineWidth)))
{
UIColor *lineColor = [self.dataSource drawingView:self lineColorForShapeAtIndex:shapeIndex];
[lineColor setStroke];
[path stroke];

if (shapeIndex == indexOfSelectedShape) {
CGFloat dashStyle[] = { 5.0f, 5.0f };
UIBezierPath *pathCopy = [path copy];
[pathCopy setLineDash:dashStyle count:2 phase:0];
[pathCopy stroke];
} else {
[path stroke];
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions PathHitTesting/ViewController.m
Expand Up @@ -103,6 +103,15 @@ - (void)addShape:(Shape *)newShape
[self.drawingView reloadDataInRect:newShape.totalBounds];
}

- (void)setSelectedShape:(Shape *)selectedShape
{
CGRect oldSelectionBounds = self.selectedShape.totalBounds;
CGRect newSelectionBounds = selectedShape.totalBounds;
CGRect rectToRedraw = CGRectUnion(oldSelectionBounds, newSelectionBounds);
_selectedShape = selectedShape;
[_drawingView setNeedsDisplayInRect:rectToRedraw];
}


#pragma mark - Hit Testing

Expand Down Expand Up @@ -170,4 +179,9 @@ - (UIColor *)drawingView:(DrawingView *)drawingView lineColorForShapeAtIndex:(NS
return shape.lineColor;
}

- (NSUInteger)indexOfSelectedShapeInDrawingView:(DrawingView *)drawingView
{
return [self.shapes indexOfObject:self.selectedShape];
}

@end

0 comments on commit 528223d

Please sign in to comment.