CGContextRef画圆、线填充等

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、最下方点坐标。

demo:https://github.com/Lynnll/MIWaterDropDemo.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值