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];}