ios - Xcode - 围绕中心绘制圆圈

标签 ios xcode drawing core-graphics

我想知道如何围绕从控件定位中获取的点绘制圆圈。

这并没有像希望的那样工作

    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。请看下图。

enter image description here

就是这样。运行您的代码以呈现 Controller 的 View 。以下是输出:

enter image description here

您可以从 here 下载代码示例

关于ios - Xcode - 围绕中心绘制圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24954735/

相关文章:

swift - 将 AVTimedMetadataGroup 添加到 AVMutableComposition 以在(临时)视频中创建章节标记

ios - 我的 Swift 获取请求返回具有错误关系的对象

iphone - 尝试让网络错误警报显示在 iPhone 应用程序上?

java - 在矩形内绘制矩形

ios - 较旧的项目停止使用最新的模拟器更新进行构建

ios - SKSpriteNodes 的内存使用

python - turtle 图形 - 间距绘制形状

objective-c - NSScrollView 问题

ios - SSL iOS9 SSL 握手失败。我检查了 openssl & 我的服务器使用 tls 1.2V

ios - 如何以编程方式调用 TabBar 的编辑模式?