1.使用 UIGraphicsGetCurrentContext() 畫圓c#
CGContextAddArc(<#CGContextRef _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)app
x,y爲圓點座標,radius半徑,startAngle爲開始的弧度,endAngle爲 結束的弧度,clockwise 0爲順時針,1爲逆時針。dom
-(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]; }