.h 文件
// // MaricoCircleProgressView.h // sb // // Created by Augussun on 16/4/28. // Copyright © 2016年 QL. All rights reserved. // #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> @interface MaricoCircleProgressView : UIView { CAShapeLayer * _trackLayer; UIBezierPath * _trackPath; CAShapeLayer * _progressLayer; UIBezierPath * _progressPath; } /// 底层圆的颜色 @property (nonatomic, strong) UIColor * trackColor; /// 上层圆的颜色 @property (nonatomic, strong) UIColor * progressColor; /// 进度 0~1 之间的数 @property (nonatomic, assign) float progress; /// 两个圆的宽度 @property (nonatomic, assign) float progressWidth; @end
.m文件
// // MaricoCircleProgressView.m // sb // // Created by Marico on 16/4/28. // Copyright © 2016年 Marico. All rights reserved. // #import "MaricoCircleProgressView.h" @implementation MaricoCircleProgressView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor purpleColor]; _trackLayer = [[CAShapeLayer alloc]init]; [self.layer addSublayer:_trackLayer]; _trackLayer.fillColor = nil; _trackLayer.frame = self.bounds; _progressLayer = [[CAShapeLayer alloc]init]; [self.layer addSublayer:_progressLayer]; _progressLayer.fillColor = nil; _progressLayer.lineCap = kCALineCapRound; _progressLayer.frame = self.bounds; self.progress = 4.f; } return self; } - (void)setTrack { _trackPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/2 startAngle:0 endAngle:M_PI*2 clockwise:YES]; _trackLayer.path = _trackPath.CGPath; } - (void)setProgress { _progressPath = [UIBezierPath bezierPathWithArcCenter:self.center radius:(self.bounds.size.width - _progressWidth)/2 startAngle:-M_PI_2 endAngle:(M_PI * 2)* _progress - M_PI_2 clockwise:YES]; _progressLayer.path = _progressPath.CGPath; } - (void)setProgressWidth:(float)progressWidth { _progressWidth = progressWidth; _trackLayer.lineWidth = _progressWidth; _progressLayer.lineWidth = _progressWidth; [self setTrack]; [self setProgress]; } - (void)setTrackColor:(UIColor *)trackColor { _trackLayer.strokeColor = trackColor.CGColor; } - (void)setProgressColor:(UIColor *)progressColor { _progressLayer.strokeColor = progressColor.CGColor; } - (void)setProgress:(float)progress { _progress = progress; [self setProgress]; } @end