如何使用核心图形绘制此弧线(包括图像)? [英] How do i draw this arc (image included) using core graphics?

查看:64
本文介绍了如何使用核心图形绘制此弧线(包括图像)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我有以下代码:

I have this code so far:

CGPoint midLeft = CGPointMake(0, additionalHeight);
    CGPoint bottomCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height);
    CGPoint midRight = CGPointMake(self.bounds.size.width, additionalHeight);
    CGPoint topRight = CGPointMake(self.bounds.size.width, 0);
    CGPoint topLeft = CGPointMake(0, 0);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    

    CGContextMoveToPoint(context, midLeft.x, midLeft.y);
    CGContextAddLineToPoint(context, midRight.x, midRight.y);
    CGContextAddLineToPoint(context, topRight.x, topRight.y);
    CGContextAddLineToPoint(context, topLeft.x, topLeft.y);

    CGContextClosePath(context);

现在,它只是向我绘制一个矩形,但我希望它向我绘制一个弧形,到达底部居中,就像这张图片顶部的形状一样:

Right now, it just draws me a rectangle, but I want it to draw me an arc, which reaches down to bottomCenter, like the shape at the top of this image:

我已经尝试了CGContextAddArc和AddArcToPoint,并按照教程进行操作,但是我不知道如何绘制这种图像弧。感谢您的帮助。

I've tried CGContextAddArc and AddArcToPoint, and followed tutorials, but I can't figure out how to draw this kind of arc. Any help is appreciated.

推荐答案

查看此博客文章,其中涵盖了弧线和路径。结果几乎与您的目标相同。

Check out this blog post, it covers arcs and paths. The result is almost the same as what you're aiming for.

我的建议是制作他的整个Core Graphics 101系列,真的很好。

My recommendation is to do his whole Core Graphics 101 series, it's really good.

您还可以使用 UIBezierPath 代替。使用 addQuadCurveToPoint:controlPoint:您可以轻松创建类似弧形的形状。

You could also use UIBezierPath instead. With addQuadCurveToPoint:controlPoint: you can easily create a arc-like shape.

我尚未测试以下代码,但应该创建像这样:

I havent tested the code below, but it should create something like this:

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 10)];
[path addQuadCurveToPoint:CGPointMake(200, 10) controlPoint:CGPointMake(100, 5)];
[path addLineToPoint:CGPointMake(200, 0)];
[path addLineToPoint:CGPointMake(0, 0)];
[path closePath];

CGContextAddPath(context, path.CGPath);
[[UIColor redColor] set];
CGContextFillPath(context);

希望对您有所帮助。

这篇关于如何使用核心图形绘制此弧线(包括图像)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆