1、画一个简单的圆
//绘制圆,不填充
- (void)drawCircleWithoutFill
{
CGContextRef context = UIGraphicsGetCurrentContext();//首先创建画布
CGContextSetStrokeColorWithColor(context, self.scoreColor.CGColor);//绘制颜色
CGContextSetLineWidth(context, 1);//线的宽度
/**
* 根据初始左边、半径、弧度画圆
*
* @param c#> 画布 description#>
* @param x#> 起始点x坐标 description#>
* @param y#> 起始点y坐标 description#>
* @param radius#> 半径 description#>
* @param startAngle#> 初始弧度,0是圆的最右边与圆心水平的点 eg:M_PI就是最左边的点 description#>
* @param endAngle#> 结束弧度 角度是从0开始顺时针计算的 description#>
* @param clockwise#> 顺时针还是逆时针,0顺时针,1逆时针 description#>
*
* @return nil
*/
CGContextAddArc(context, x, y, r, 0, M_PI / 2, 0);
CGContextDrawPath(context, kCGPathStroke);
}
效果如图:
CGContextAddArc(context, x, y, r, 0, M_PI / 2, 1);
效果如下:
ok,参数什么的大概了解了,开始画整圆吧!
CGContextAddArc(context, x, y, r, 0, 2 * M_PI, 1);
2、填充圆
//绘制圆,填充
- (void)drawCircleWithFill
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, self.scoreColor.CGColor);//设置填充颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);<span class="s1" style="font-family: Arial, Helvetica, sans-serif;">//</span><span class="s2" style="font-family: Arial, Helvetica, sans-serif;">边线颜色,需要时,与填充颜色一致</span>
CGContextSetLineWidth(context, 0.5);//填充是,边线宽度可去掉
CGContextAddArc(context, x, y, r, 0, 2 * M_PI, 1);
CGContextDrawPath(context, kCGPathFillStroke);//kCGPathFillStroke绘制模式:填充
}
效果:
接下来,画一个水滴吧。
画了一个草图以便于理解
代码如下:
//水滴,无填充
- (void)drawWaterDropWithoutFill
{
CGContextRef context = UIGraphicsGetCurrentContext();//首先创建画布
CGContextSetStrokeColorWithColor(context, self.scoreColor.CGColor);//绘制颜色
CGContextSetLineWidth(context, 1);//线的宽度
CGContextAddArc(context, x, y, r, 0 + M_PI / 6, M_PI - M_PI / 6, 1);//首先画大半个圆形
CGContextAddLineToPoint(context, x, y + (r / cosf(M_PI / 2 - angle)));//接着从start point开始画直线,目标坐标为最下方的点
CGContextAddLineToPoint(context, x + r * cosf(angle), y + r * sinf(angle));//接着以end point为终点画直线即可
CGContextDrawPath(context, kCGPathStroke);
}
几个点需要计算:
1、UIView的高度是需要计算的,高度为半径+草图中红色虚线的长度。红线长度 = r + r / (cos(90-angle));
2、start point 或 end point : 如果是从start point 开始画圆,只需要计算start point即可,反之计算end point;
3、最下方点坐标。