在iPhone中绘制空心圆

发布于 2024-11-04 05:18:08 字数 246 浏览 4 评论 0 原文

我需要绘制以下图像 在此处输入图像描述

灰色部分是我想在另一张图像上绘制的内容是什么我需要使用 CGContext 方法使用的代码,我尝试使用 CGContextAddArc 但失败了,因为当我填充笔划时,中心空心也填充了灰色纹理。

任何帮助表示赞赏。

信息:我有完整的蓝色图像,我需要在蓝色图像上方添加半圆

谢谢

i need to draw the following image enter image description here

The Gray part is what i want to draw over another image what is the Code i need to use using CGContext methods, i tried using the CGContextAddArc but failed because when i fill the stroke the center hollow is also filled with the grey texture.

Any help appreciated.

Info : I have the Complete Blue Image , i need to add the Semi Circle above the blue image

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坐在坟头思考人生 2024-11-11 05:18:08

看看 填充路径。基本上,您要做的就是在路径中添加两条弧线(外部和内部),然后使用 Core Graphics 填充规则来发挥您的优势。代码看起来像这样:

CGMutablePathRef path = CGPathCreateMutable();

// Add the outer arc to the path (as if you wanted to fill the entire circle)
CGPathMoveToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

// Add the inner arc to the path (later used to substract the inner area)
CGPathMoveToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

// Add the path to the context
CGContextAddPath(context, path);

// Fill the path using the even-odd fill rule
CGContextEOFillPath(context);

CGPathRelease(path);

Have a look at Filling a Path in the Core Graphics documentation. Basically, what you do is add two arcs to your path (the outer and the inner one) and then use Core Graphics fill rules to your advantage. The code would look something like this:

CGMutablePathRef path = CGPathCreateMutable();

// Add the outer arc to the path (as if you wanted to fill the entire circle)
CGPathMoveToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

// Add the inner arc to the path (later used to substract the inner area)
CGPathMoveToPoint(path, ...);
CGPathAddArc(path, ...);
CGPathCloseSubpath(path);

// Add the path to the context
CGContextAddPath(context, path);

// Fill the path using the even-odd fill rule
CGContextEOFillPath(context);

CGPathRelease(path);
柳絮泡泡 2024-11-11 05:18:08

进一步研究 Ole Begemann 在他的答案中提到的内容并进行一些修改,我能够实现要求。

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_grey_pattern.png"]].CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGContextSetLineWidth(context, 40);
CGPathAddArc(path, NULL, aRect.size.width/2, aRect.size.height/2, 45, 0*3.142/180, angle*3.142/180, 0);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGPathRelease(path);

因此,我没有使用 2 条弧线,而是只使用了 1 条弧线,并以更大的宽度描画它。

Working further on what Ole Begemann referred in his answer and some modification, I was able to achieve the requirement.

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:@"background_grey_pattern.png"]].CGColor);
CGMutablePathRef path = CGPathCreateMutable();
CGContextSetLineWidth(context, 40);
CGPathAddArc(path, NULL, aRect.size.width/2, aRect.size.height/2, 45, 0*3.142/180, angle*3.142/180, 0);
CGContextAddPath(context, path);
CGContextStrokePath(context);
CGPathRelease(path);

So instead of 2 arcs I used only one and stroked it with higher width.

涫野音 2024-11-11 05:18:08

这是切线但有点相关的。这是我的甜甜圈(或空心圆)的 Swift 绘制代码。

class func drawDonut(donutColor: UIColor, rect: CGRect, innerDonutProportion: CGFloat) {

    //// Variable Declarations
    let innerDonutSide: CGFloat = rect.size.width * innerDonutProportion
    let innerDonutOffset: CGFloat = 0.5 * (rect.size.width - innerDonutSide)
    let innerDonutRect = CGRectMake(innerDonutOffset, innerDonutOffset, innerDonutSide, innerDonutSide)

    //// InnerCircle Drawing
    let innerCirclePath = UIBezierPath(ovalInRect: innerDonutRect)

    //// Outer Circle Drawing
    let outerCirclePath = UIBezierPath(ovalInRect: rect)

    // Clip out the innerCirclePath
    outerCirclePath.appendPath(innerCirclePath)
    outerCirclePath.addClip()
    outerCirclePath.usesEvenOddFillRule = true;

    // and Fill the outerCircle
    donutColor.setFill()
    outerCirclePath.fill()
}

This is tangential but kind of relevant. Here's my Swift drawing code for a donut (or hollow circle).

class func drawDonut(donutColor: UIColor, rect: CGRect, innerDonutProportion: CGFloat) {

    //// Variable Declarations
    let innerDonutSide: CGFloat = rect.size.width * innerDonutProportion
    let innerDonutOffset: CGFloat = 0.5 * (rect.size.width - innerDonutSide)
    let innerDonutRect = CGRectMake(innerDonutOffset, innerDonutOffset, innerDonutSide, innerDonutSide)

    //// InnerCircle Drawing
    let innerCirclePath = UIBezierPath(ovalInRect: innerDonutRect)

    //// Outer Circle Drawing
    let outerCirclePath = UIBezierPath(ovalInRect: rect)

    // Clip out the innerCirclePath
    outerCirclePath.appendPath(innerCirclePath)
    outerCirclePath.addClip()
    outerCirclePath.usesEvenOddFillRule = true;

    // and Fill the outerCircle
    donutColor.setFill()
    outerCirclePath.fill()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文