6

I am trying to draw a semicircle using Core Graphics and Filling it with some color.I want to replace color with image by using CALayer. Can any one help how to do this Thanks!

Code goes here.

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 0, 500);
CGContextAddArc(context, 60, 500, 50, 90, 180, 0);
CGContextStrokePath(context);
  • 1
    This link may help you : http://stackoverflow.com/questions/3729690/draw-part-of-a-circle and also check : http://stackoverflow.com/questions/2835448/how-to-draw-a-rounded-rectangle-in-core-graphics-quartz-2d – Devang Mar 30 '12 at 06:39

4 Answers4

7

You need to specify the angles in radians, not degrees. 180 degrees = π radians. So in your example:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 10, 500);
CGContextAddArc(context, 60, 500, 50, M_PI / 2, M_PI, 0);
CGContextStrokePath(context);

Note that I also moved the starting point (…MoveToPoint) 10 pixels to the right, because that’s where the arc begins.

Sijmen Mulder
  • 5,767
  • 3
  • 22
  • 33
5

The given answers are not working on iOS 7 for me!
I created a little category to create a circle with the angle of a percentage. Maybe you wanna check it out.
You can still adopt the methode if you only want to create a semicircle.
Here is the: GitHub Link
And that is what its look like:

persentage circle

zero3nna
  • 2,770
  • 30
  • 28
0
CGPoint circleCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);


CGContextAddArc(context, circleCenter.x, circleCenter.y, 50, 90, 180, 0);
CGContextAddLineToPoint(context, circleCenter.x, circleCenter.y);
CGContextSetFillColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:@"add-icon.png"]].CGColor);
CGContextFillPath(context);

CGContextAddArc(context, circleCenter.x, circleCenter.y, 50, 180, 90, 0);
CGContextAddLineToPoint(context, circleCenter.x, circleCenter.y);
CGContextSetFillColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:@"appIcon_58.png"]].CGColor);
CGContextFillPath(context);
Mohamad Chami
  • 1,226
  • 14
  • 10
-2

What you need, which I found the other day, is a great tool that lets you visualise Core Graphics code - after drawing it in a graphics program.

Its called Paint Code.

PaintCode

Bart Jacobs
  • 9,022
  • 7
  • 47
  • 88
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • Can Piantcode be used to create custom controls. Say I want to draw a filled arc with a text in the middle of the arc that I want to change at runtime. Effectively a new control of sorts. Can PaintCode do that? Not very obvious by looking at their web site? – OneGuyInDc Jan 11 '13 at 18:25
  • Install it (the demo) and see. I don't know. – bandejapaisa Jan 15 '13 at 16:08