xcode 如何使用 CoreGraphics 绘制半圆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9937964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:50:13  来源:igfitidea点击:

How can i draw a semicircle using CoreGraphics

iphoneiosxcode

提问by Sijmen Mulder

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!

我正在尝试使用 Core Graphics 绘制一个半圆并用一些颜色填充它。我想使用 CALayer 用图像替换颜色。任何人都可以帮助如何做到这一点谢谢!

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

回答by Sijmen Mulder

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

您需要以弧度而不是度数指定角度。180 度 = π 弧度。所以在你的例子中:

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 个像素,因为这是圆弧的起点。

回答by zero3nna

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:

给定的答案对我来说不适用于 iOS 7!
我创建了一个小类别来创建一个带有百分比角度的圆。也许你想看看。
如果您只想创建一个半圆,您仍然可以采用该方法。
这是:GitHub 链接
这就是它的样子:

persentage circle

代表圈

回答by Mohamad Chami

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

回答by bandejapaisa

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.

我前几天发现,您需要的是一个很棒的工具,它可以让您在图形程序中绘制 Core Graphics 代码后进行可视化。

Its called Paint Code.

它被称为油漆代码。

PaintCode

油漆代码