我想知道如何围绕从控件定位中获取的点绘制圆圈。
这并没有像希望的那样工作
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 2.0);
CGContextSetRGBFillColor(contextRef, 0, 0, 1.0, 1.0);
CGContextSetRGBStrokeColor(contextRef, 0, 0, 1.0, 1.0);
CGRect circlePoint = (CGRectMake(self.frame.size.width / 2, self.frame.size.height / 2, 10.0, 10.0));
CGContextFillEllipseInRect(contextRef, circlePoint);
最佳答案
我认为使用 UIBezierPath 绘制圆非常容易。您需要做的就是子类化 UIView 并创建 UIBezierPath。我使用 UIBezierPath 创建了一个示例:
创建一个名为 CirecleView 的 UIView 的子类。添加以下代码:
#import "CircleView.h"
@implementation CircleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGFloat rectX = self.frame.size.width / 2;
CGFloat rectY = self.frame.size.height / 2;
CGFloat width = 100;
CGFloat height = 100;
CGFloat centerX = rectX - width/2;
CGFloat centerY = rectY - height/2;
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(centerX, centerY, width, height)];
[[UIColor blackColor] set];
[bezierPath stroke];
}
@end
将 Controller 的 View 类更改为 CircleView。请看下图。
就是这样。运行您的代码以呈现 Controller 的 View 。以下是输出:
您可以从 here 下载代码示例
关于ios - Xcode - 围绕中心绘制圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24954735/