2019獨角獸企業重金招聘Python工程師標準>>> 
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];
} ?