#import "ScorePlateView.h"
@interface ScorePlateView()
{
CGFloat d_speed;
CGFloat d_altitude;
CGFloat d_comp;
}
@property (nonatomic,strong) UILabel*lbeCompreValue;
@property (nonatomic,strong) UILabel *compreValue;
@property (nonatomic,strong) UILabel * comment;
@property (nonatomic,assign) CGFloat cur_speedV;
@property (nonatomic,assign) CGFloat cur_altitudeV;
@property (nonatomic,assign) CGFloat cur_compV;
@property (nonatomic,assign) NSTimer * timer;
@end
@implementation ScorePlateView
- (instancetype)initWithFrame:(CGRect)frame
{
if
(self = [super initWithFrame:frame]) {
self.precision= 50;
self.fullValues =5;
self.altitudeValues=3.0;
self.speedValues=4.0;
self.backgroundColor = [UIColor clearColor];
self.startAngle=0.1*M_PI;
self.endAngle = 0.9*M_PI;
self.comments =@
"真是太不可思议了"
;
self.backgroundColor = [UIColor greenColor];
_cur_compV=0;
_cur_speedV=0;
_cur_altitudeV=0;
}
return
self;
}
- (
void
)drawRect:(CGRect)rect {
CGFloat circleMargin = 0;
CGFloat topBottomMargin =20;
CGFloat radius = (self.frame.size.height-circleMargin-2*topBottomMargin)/2;
CGPoint centerTop = CGPointMake(self.frame.size.width/2,self.frame.size.height/2-circleMargin/2);
[self drawHalfCircle:centerTop andWithRadius:radius isTop:YES];
CGPoint centerBottom = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+circleMargin/2);
[self drawHalfCircle:centerBottom andWithRadius:radius isTop:NO];
UILabel * lbeAltitude = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, topBottomMargin)];
lbeAltitude.text = @
"服务速度"
;
lbeAltitude.textColor = [UIColor whiteColor];
lbeAltitude.textAlignment = NSTextAlignmentCenter;
lbeAltitude.font = [UIFont systemFontOfSize:12];
[lbeAltitude drawTextInRect:lbeAltitude.frame];
UILabel * lbeSpeed = [[UILabel alloc]initWithFrame:CGRectMake(0, self.frame.size.height-topBottomMargin, self.frame.size.width, topBottomMargin)];
lbeSpeed.text = @
"服务态度"
;
lbeSpeed.textColor = [UIColor whiteColor];
lbeSpeed.textAlignment = NSTextAlignmentCenter;
lbeSpeed.font = [UIFont systemFontOfSize:12];
[lbeSpeed drawTextInRect:lbeSpeed.frame];
NSString *attitudeScore = [NSString stringWithFormat:@
"%.2f/%.2f"
,_cur_altitudeV,self.fullValues];
NSMutableAttributedString* attributeString = [[NSMutableAttributedString alloc]initWithString:attitudeScore];
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:26] range:NSMakeRange(0, 4)];
[attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(4, 3)];
self.compreValue = [[UILabel alloc]initWithFrame:CGRectMake(0, radius-topBottomMargin,self.frame.size.width, 2*topBottomMargin)];
self.compreValue.attributedText = attributeString;
self.compreValue.textAlignment = NSTextAlignmentCenter;
self.compreValue.textColor = [UIColor whiteColor];
self.compreValue.frame = CGRectMake(0, centerTop.y-topBottomMargin+circleMargin/2, self.frame.size.width, topBottomMargin*2);
[self.compreValue drawTextInRect:self.compreValue.frame];
self.lbeCompreValue = [[UILabel alloc]initWithFrame:CGRectMake(0, centerTop.y-radius*0.5, self.frame.size.width, topBottomMargin*2)];
self.lbeCompreValue.text =@
"综合评分"
;
self.lbeCompreValue.textAlignment = NSTextAlignmentCenter;
self.lbeCompreValue.textColor = [UIColor whiteColor];
self.lbeCompreValue.font = [UIFont systemFontOfSize:14];
[self.lbeCompreValue drawTextInRect:self.lbeCompreValue.frame];
self.comment = [[UILabel alloc]initWithFrame:CGRectMake(topBottomMargin+circleMargin+radius/2, CGRectGetMaxY(self.compreValue.frame), radius, topBottomMargin*2)];
self.comment.text =self.comments;
self.comment.numberOfLines=0;
self.comment.textAlignment = NSTextAlignmentCenter;
self.comment.textColor = [UIColor whiteColor];
self.comment.font = [UIFont systemFontOfSize:14];
[self.comment drawTextInRect:self.comment.frame];
}
-(
void
)drawHalfCircle:(CGPoint) center andWithRadius:(CGFloat)radius isTop:(
BOOL
) top
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
if
(top) {
CGContextAddArc(ctx, center.x, center.y, radius, -self.startAngle, -self.endAngle, 1);
}
else
{
CGContextAddArc(ctx, center.x, center.y, radius,self.startAngle,self.endAngle, 0);
}
CGContextSetLineWidth(ctx, 1);
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
CGContextStrokePath(ctx);
CGContextSetLineWidth(ctx, 2);
CGFloat borderValue;
if
(top) {
borderValue=_cur_altitudeV/self.fullValues;
}
else
{
borderValue =_cur_speedV/self.fullValues;
}
for
(
int
i=1; i<self.precision; i++)
{
CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 0.5);
CGFloat endX,endY,startX,startY;
if
(top) {
startX = center.x -(radius-10)*
cos
((
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);
startY = center.y - (radius-10)*
sin
((
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);
endX = center.x - (radius-2)*
cos
((
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);
endY = center.y - (radius-2)*
sin
((
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);
}
else
{
startX = center.x +(radius-10)*
cos
((
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);
startY = center.y + (radius-10)*
sin
((
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);
endX = center.x + (radius-2)*
cos
((
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);
endY = center.y + (radius-2)*
sin
((
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle);
}
CGContextMoveToPoint(ctx, startX, startY);
CGContextAddLineToPoint(ctx, endX, endY);
CGContextStrokePath(ctx);
}
for
(
int
i=1; i<self.precision; i++)
{
CGFloat curAngle =(
double
)i/self.precision*(self.endAngle-self.startAngle)+self.startAngle;
if
((
double
)i/(
double
)self.precision<borderValue)
{
CGContextSetRGBStrokeColor(ctx, 1.0, 1.0, 1.0, 1);
CGFloat endX,endY,startX,startY;
if
(top) {
startX = center.x -(radius-10)*
cos
(curAngle);
startY = center.y - (radius-10)*
sin
(curAngle);
endX = center.x - (radius-2)*
cos
(curAngle);
endY = center.y - (radius-2)*
sin
(curAngle);
}
else
{
startX = center.x +(radius-10)*
cos
(curAngle);
startY = center.y + (radius-10)*
sin
(curAngle);
endX = center.x + (radius-2)*
cos
(curAngle);
endY = center.y + (radius-2)*
sin
(curAngle);
}
CGContextMoveToPoint(ctx, startX, startY);
CGContextAddLineToPoint(ctx, endX, endY);
CGContextStrokePath(ctx);
}
}
}
- (
void
)didMoveToSuperview
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES];
d_comp = self.compreValues/20;
d_speed= self.speedValues/20;
d_altitude=self.speedValues/20;
}
-(
void
)update
{
_cur_altitudeV+=d_altitude;
_cur_speedV+=d_speed;
_cur_compV+=d_comp;
if
(_cur_altitudeV>self.altitudeValues) {
_cur_altitudeV =self.altitudeValues;
}
if
(_cur_speedV > self.speedValues) {
_cur_speedV=self.speedValues;
}
if
(_cur_compV>self.compreValues) {
_cur_compV=self.compreValues;
}
if
( _cur_compV==self.compreValues&&_cur_speedV==self.speedValues&&_cur_altitudeV ==self.altitudeValues) {
[self.timer invalidate];
self.timer = nil;
}
[self setNeedsDisplay];
}
@end