How can I draw a semicircle using CoreGraphics

I am trying to make a semicircle using Core Graphics and filling it with some color. I want to replace color with an image using CALayer. Can anyone help how to do this? Thanks!

Here comes the code.

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); 
+6
source share
4 answers

You need to specify angles in radians , not degrees. 180 degrees = π rad. 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 the arc starts there.

+7
source

These answers do not work on iOS 7 for me!
I created a small category to create a circle with a percentage angle. Maybe you want to check it out.
You can still use the method if you want to create a semicircle. Here is the link: GitHub link
And here is what it looks like:

persentage circle

+5
source
 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); 
0
source

What you need, what I found the other day, is an excellent tool that allows you to visualize Core Graphics code - after drawing it in a graphics program.

Its called Paint Code.

Paintcode

-2
source

Source: https://habr.com/ru/post/912022/


All Articles