Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add another custom shape named "arc"

  • Loading branch information...
commit bde777aea31454f8991feee1a37b52492dc161c4 1 parent badf32d
Ole Begemann authored

Showing 2 changed files with 29 additions and 0 deletions. Show diff stats Hide diff stats

  1. +1 0  PathHitTesting/Shape.h
  2. +28 0 PathHitTesting/Shape.m
1  PathHitTesting/Shape.h
@@ -12,6 +12,7 @@ typedef enum {
12 12 ShapeTypeRect,
13 13 ShapeTypeEllipse,
14 14 ShapeTypeHouse,
  15 + ShapeTypeArc,
15 16 SHAPE_TYPE_COUNT
16 17 } ShapeType;
17 18
28 PathHitTesting/Shape.m
@@ -19,6 +19,7 @@ + (CGRect)randomRectInBounds:(CGRect)maxBounds;
19 19 + (UIColor *)randomColor;
20 20 + (CGFloat)randomLineWidth;
21 21 + (UIBezierPath *)houseInRect:(CGRect)bounds;
  22 ++ (UIBezierPath *)arcInRect:(CGRect)bounds;
22 23
23 24 @end
24 25
@@ -46,6 +47,9 @@ + (id)randomShapeInBounds:(CGRect)maxBounds
46 47 case ShapeTypeHouse:
47 48 path = [self houseInRect:bounds];
48 49 break;
  50 + case ShapeTypeArc:
  51 + path = [self arcInRect:bounds];
  52 + break;
49 53 default:
50 54 path = [UIBezierPath bezierPathWithRect:bounds];
51 55 break;
@@ -222,4 +226,28 @@ + (UIBezierPath *)houseInRect:(CGRect)bounds
222 226 return path;
223 227 }
224 228
  229 ++ (UIBezierPath *)arcInRect:(CGRect)bounds
  230 +{
  231 + CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
  232 + CGPoint centerRight = CGPointMake(CGRectGetMaxX(bounds), CGRectGetMidY(bounds));
  233 + CGFloat radius = CGRectGetWidth(bounds) / 2.0f;
  234 + CGPoint center2 = CGPointMake(center.x, center.y - radius / 2.0f);
  235 +
  236 + UIBezierPath *path = [UIBezierPath bezierPath];
  237 + [path moveToPoint:centerRight];
  238 + [path addArcWithCenter:center radius:radius startAngle:0.0f endAngle:M_PI * 1.5f clockwise:YES];
  239 + [path addArcWithCenter:center2 radius:radius / 2.0f startAngle:M_PI * 1.5f endAngle:M_PI clockwise:YES];
  240 +
  241 + path.lineJoinStyle = kCGLineJoinRound;
  242 +
  243 + CGAffineTransform transform = CGAffineTransformIdentity;
  244 + transform = CGAffineTransformTranslate(transform, bounds.origin.x, bounds.origin.y);
  245 + transform = CGAffineTransformTranslate(transform, 0.0, bounds.size.height);
  246 + transform = CGAffineTransformScale(transform, 1.0f, -1.0f);
  247 + transform = CGAffineTransformTranslate(transform, -bounds.origin.x, -bounds.origin.y);
  248 + [path applyTransform:transform];
  249 +
  250 + return path;
  251 +}
  252 +
225 253 @end

0 comments on commit bde777a

Please sign in to comment.
Something went wrong with that request. Please try again.