打轉顯示

//
//  CircularProgressView.h
//  TestProject
//
//  Created by lance on 14-5-13.
//  Copyright (c) 2014年 Lance. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CircularProgressView : UIView
{
    BOOL _canDraw;
}

@property (nonatomic, strong) UIColor *trackTintColor;
@property (nonatomic, strong) UIColor *progressTintColor;

@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, strong) UILabel *infoLabel;
@property (nonatomic, strong) UILabel *gradeLabel;
@property (nonatomic, strong) UILabel *unitLabel;

@end

//
//  CircularProgressView.m
//  TestProject
//
//  Created by lance on 14-5-13.
//  Copyright (c) 2014年 Lance. All rights reserved.
//

#import "CircularProgressView.h"

#define DEGREES_2_RADIANS(x) (0.0174532925 * (x))

@implementation CircularProgressView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        
        _canDraw = NO;
        
        // 半徑
        _infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width / 2 - 40.0, frame.size.height * 0.3 - 30 , 80,  40)];
        _infoLabel.textColor = [UIColor whiteColor];
        _infoLabel.backgroundColor = [UIColor clearColor];
        _infoLabel.font = [UIFont boldSystemFontOfSize:16.0];
        _infoLabel.textAlignment = NSTextAlignmentCenter;
        _infoLabel.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.4];
        _infoLabel.text = @"駕駛評分";
        [self addSubview:_infoLabel];
        
        _gradeLabel = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width / 2 - 60, CGRectGetMaxY(_infoLabel.frame) - 5.0f, 120, 70)];
        _gradeLabel.textColor = [UIColor whiteColor];
        _gradeLabel.font = [UIFont systemFontOfSize:70.0f];
        _gradeLabel.textAlignment = NSTextAlignmentCenter;
        _gradeLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_gradeLabel];
        
        _unitLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_gradeLabel.frame) - 2.0f, CGRectGetMaxY(_gradeLabel.frame) - 25, 20, 20)];
        _unitLabel.text = @"分";
        _unitLabel.font = [UIFont systemFontOfSize:14.0f];
        _unitLabel.textColor = [UIColor whiteColor];
        _unitLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_unitLabel];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // 中心點
    CGPoint centerPoint = CGPointMake(rect.size.height / 2, rect.size.width / 2);
    // 路徑寬度
    CGFloat radius = MIN(rect.size.height, rect.size.width) / 2;
    CGFloat pathWidth = radius * 0.1;
    CGFloat radians = DEGREES_2_RADIANS((self.progress * 359.9) - 90);
    CGFloat xOffset = radius * ( 1 + 0.95 * cosf(radians));
    CGFloat yOffset = radius * ( 1 + 0.95 * sinf(radians));
    CGPoint endPoint = CGPointMake(xOffset, yOffset);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
   
    [self.trackTintColor setFill];
    CGMutablePathRef trackPathRef = CGPathCreateMutable();
    CGPathMoveToPoint(trackPathRef, NULL, centerPoint.x, centerPoint.y);
    CGPathAddArc(trackPathRef, NULL, centerPoint.x, centerPoint.y, radius - 5, DEGREES_2_RADIANS(270), DEGREES_2_RADIANS(-90), NO);
    CGPathCloseSubpath(trackPathRef);
    CGContextAddPath(context, trackPathRef);
    CGContextFillPath(context);
    CGPathRelease(trackPathRef);
    
    // progressView
    [self.progressColor setFill];
    
    if (_canDraw) {
        CGMutablePathRef progressPath = CGPathCreateMutable();
        CGPathMoveToPoint(progressPath, NULL, centerPoint.x, centerPoint.y);
        CGPathAddArc(progressPath, NULL, centerPoint.x, centerPoint.y, radius, DEGREES_2_RADIANS(270), radians, NO);
        CGPathCloseSubpath(progressPath);
        CGContextAddPath(context, progressPath);
        CGContextFillPath(context);
        CGPathRelease(progressPath);
        
        CGContextAddEllipseInRect(context, CGRectMake(centerPoint.x - pathWidth / 2, 0, pathWidth, pathWidth));
        CGContextFillPath(context);
        
        CGContextAddEllipseInRect(context, CGRectMake(endPoint.x - pathWidth / 2, endPoint.y - pathWidth / 2, pathWidth, pathWidth));
        CGContextFillPath(context);
    } else {
        _canDraw = YES;
    }
    
    CGContextSetBlendMode(context, kCGBlendModeClear); //是否使用混合顏色
    CGFloat innerRadius = radius * 0.9;
    CGPoint newCenterPoint = CGPointMake(centerPoint.x - innerRadius, centerPoint.y - innerRadius);
    CGContextAddEllipseInRect(context, CGRectMake(newCenterPoint.x, newCenterPoint.y, innerRadius * 2, innerRadius * 2));
    CGContextFillPath(context);
}

- (UIColor *)trackTintColor
{
    if (!_trackTintColor) {
        _trackTintColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.3];
    }
    return _trackTintColor;
}

- (UIColor *)progressColor
{
    if (!_progressTintColor) {
        _progressTintColor = [UIColor whiteColor];
    }
    return _progressTintColor;
}

- (void)setProgress:(CGFloat)progress
{
    if (progress == 0) {
        _canDraw = NO;
    }
    
    _progress = progress;
    
    [self setNeedsDisplay];
}

@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章