Will Roberts
Will Roberts

Reputation: 319

appendPath to produce hollow object Core Graphics

So I am trying to create a segment of a circle ie a pie slice then use a circle to remove the majority of the wedge leaving an outer arc.

This is what I have so farImage As you can see it is messing up somewhere!

I achieve this using the following code:-

UIBezierPath* theStroke = [UIBezierPath bezierPathWithRoundedRect:mainCutout cornerRadius:theRadius];
[theOrangeColor setFill];
theStroke.usesEvenOddFillRule = YES;
[theStroke fill];
[theStroke stroke];
[theStroke setMiterLimit:2.0];


UIBezierPath *aSegment = [UIBezierPath bezierPath];
aSegment.usesEvenOddFillRule = YES;

[aSegment moveToPoint:theCenter];
[aSegment addLineToPoint:theCenter];
[aSegment addArcWithCenter:theCenter radius:theRadius startAngle:startAngle endAngle:endAngle clockwise:YES];
[aSegment addLineToPoint:theCenter];
[aSegment appendPath:theStroke];

[theRedColor setFill];
[aSegment fill];
[aSegment stroke];
[aSegment closePath];

Can anyone help me?

Upvotes: 0

Views: 764

Answers (2)

Ram Gandhi
Ram Gandhi

Reputation: 717

With a slight modification in @Martin R's code, If you can specify the lineWidth of the CALayer over which you gonna draw your arc segment, a single "addArcWithCenter:radius:startAngle:endAngle:clockwise:" will do the job for you.

You have to use stroke instead of fill.(i.e.)just use strokeColor

Eg:

static inline double radians (double degrees)
{
    return degrees * M_PI/180;
}

CAShapeLayer * shapeLayer = [CAShapeLayer layer];
shapeLayer.lineWidth = 10;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.strokeColor = [UIColor redColor].CGColor;
CGFloat radius = 50.0;
shapeLayer.path = [[UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:radians(startingAngle) endAngle:radians(endingAngle) clockwise:1 ]CGPath ];
[self.layer addSublayer:shapeLayer];

This may be useful to someone...

Upvotes: 0

Martin R
Martin R

Reputation: 539725

I do not see how you can use the even-odd filling rule to remove that parts of the sector.

But you can easily draw the "outer slice" of the segment by drawing two arc segments with different radii. Example:

CGPoint theCenter = CGPointMake(100., 100.);
CGFloat innerRadius = 50.;
CGFloat outerRadius = 60.;
CGFloat startAngle = M_PI;
CGFloat endAngle = 3*M_PI/2;

UIBezierPath *aSegment = [UIBezierPath bezierPath];
[aSegment addArcWithCenter:theCenter radius:innerRadius startAngle:startAngle endAngle:endAngle clockwise:YES];
[aSegment addArcWithCenter:theCenter radius:outerRadius startAngle:endAngle endAngle:startAngle clockwise:NO];
[aSegment closePath];
[[UIColor redColor] setFill];
[aSegment fill];

Result:

enter image description here

Upvotes: 4

Related Questions