//
// CircleView.h
//
// Created by Sabrina on 2017/5/12.
// Copyright © 2017年 Sabrina All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CircleView : UIView
@property (nonatomic) CGFloat precent;
- (id)initWithFrame:(CGRect)frame arcWidth:(double)width;
@end
//
// CircleView.m
//
// Created by Sabrina on 2017/5/12.
// Copyright © 2017年 Sabrina All rights reserved.
//
#import "CircleView.h"
@implementation CircleView{
CGFloat startAngle;
CGFloat endAngle;
CGFloat circlewWidth, lineWith;
}
- (id)initWithFrame:(CGRect)frame arcWidth:(double)width
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
// Determine our start and stop angles for the arc (in radians)
startAngle = M_PI * 1.5;
endAngle = startAngle + (M_PI * 2);
circlewWidth = self.frame.size.width - 20;
lineWith = self.frame.size.width / 2;
}
return self;
}
- (void)drawRect:(CGRect)rect {
UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, circlewWidth, circlewWidth)];
[[UIColor blueColor] setStroke];
circle.lineWidth = 0;
[circle stroke];
[[UIColor whiteColor] setFill];
[circle fill];
UIBezierPath *arcLine = [UIBezierPath bezierPathWithArcCenter:CGPointMake(lineWith, lineWith) radius:circlewWidth / 2 startAngle:startAngle endAngle:(endAngle - startAngle) * (self.precent / 100.0) + startAngle clockwise:YES];
arcLine.lineWidth = 10;
[[UIColor colorWithRed:145/255.0 green:197/255.0 blue:226/255.0 alpha:1.0] setStroke];
[arcLine stroke];
NSString* textContent = @"";
if (self.precent > 100.0){
textContent = [NSString stringWithFormat:@"%@%%", @"100"];
}else{
textContent = [NSString stringWithFormat:@"%.1f%%", self.precent];
}
// Text Drawing
CGRect textRect = CGRectMake((rect.size.width / 2.0)/2.0, (rect.size.height / 2.0) - 45/2.0, circlewWidth, 45);
[[UIColor colorWithRed:145/255.0 green:197/255.0 blue:226/255.0 alpha:1.0] setFill];
NSDictionary *attributes = @{ NSFontAttributeName: [UIFont fontWithName: @"Helvetica-Bold" size: 30.0],
NSForegroundColorAttributeName : [UIColor colorWithRed:145/255.0 green:197/255.0 blue:226/255.0 alpha:1.0]};
[textContent drawInRect:textRect withAttributes:attributes];
}
@end