简体   繁体   English

如何使用CoreGraphics绘制半圆

[英]How can i draw a semicircle using CoreGraphics

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. 我正在尝试使用Core Graphics绘制一个半圆并用一些颜色填充它。我想用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);

You need to specify the angles in radians , not degrees. 您需要以弧度为单位指定角度,而不是度数。 180 degrees = π radians. 180度=π弧度。 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. 请注意,我还将起点(... MoveToPoint)向右移动了10个像素,因为这是弧开始的位置。

The given answers are not working on iOS 7 for me! 给出的答案对我来说不适用于iOS 7!
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 这是: GitHub链接
And that is what its look like: 这就是它的样子:

persentage圈

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);

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 PaintCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM