打转显示

//
//  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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值