iOS绘圆形图-CGContextAddArc各参数说明

1.使用 UIGraphicsGetCurrentContext() 画圆

    CGContextAddArc(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)

x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。

-(void)drawRect:(CGRect)rect{
    CGFloat lineWidth = 12;
    NSInteger sleepScore = arc4random() % 101;
    //定义一个不透明类型的Quartz 2D绘画环境,相当于一个画布
    CGContextRef context = UIGraphicsGetCurrentContext();
    [[UIColor whiteColor] set];
    CGContextFillRect(context, rect);
    //有值圆
    CGFloat scoreLine = sleepScore/100.0;
    Log(@"sleepScore %ld %f",sleepScore,scoreLine);
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithHexString:@"#48BBAD"].CGColor);
    CGContextSetLineWidth(context, lineWidth);
    CGContextAddArc(context, rect.size.width/2,rect.size.height, rect.size.width/2-lineWidth/2, -M_PI, -M_PI/4, 0);// 0为顺时针,1为逆时针
    CGContextDrawPath(context, kCGPathStroke);
    /*写分数*/
    CGFloat fontSize = (rect.size.height-lineWidth)*0.7;
    CGRect scoreRect = CGRectMake(rect.size.width/2-fontSize, rect.size.height-fontSize, fontSize*2, fontSize);
    [self drawTextWithContent:@"100" WithFontSize:fontSize WithWeight:1 withTextColor:[UIColor blackColor] withRect:scoreRect withAlignment:NSTextAlignmentCenter];
}
-(void)drawTextWithContent:(NSString *)text WithFontSize:(CGFloat)fontSize WithWeight:(CGFloat)weight withTextColor:(UIColor *)textColor withRect:(CGRect)rect withAlignment:(NSTextAlignment)alignment{
    //    Log(@"fontSize:%f",fontSize);
    NSMutableDictionary *attributesDic = [NSMutableDictionary dictionary];
    attributesDic[NSFontAttributeName] = [UIFont systemFontOfSize:fontSize weight:weight];
    attributesDic[NSForegroundColorAttributeName] = textColor;
    //段落格式
    NSMutableParagraphStyle *textStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    textStyle.lineBreakMode = NSLineBreakByWordWrapping;
    textStyle.alignment = alignment;//对齐方式
    attributesDic[NSParagraphStyleAttributeName] = textStyle;
    [text drawInRect:rect withAttributes:attributesDic];
}

 

转载于:https://my.oschina.net/u/2365397/blog/1583670

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值