自定义环形进度条
思路:
1.利用
直接附上代码
test.h/test.m
MyProgressView.h/MyProgressView.m
#import <UIKit/UIKit.h>
@interface test : UIView
@property(nonatomic, assign)float progressRate;
@end
#import "test.h"
#import <QuartzCore/QuartzCore.h>
@implementation test
-(void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor (context, 1, 0, 0, 1.0);//设置填充颜色
//画扇形并填充颜
UIColor*aColor = [UIColor lightGrayColor];
CGContextSetFillColorWithColor(context, aColor.CGColor);//填充颜色
CGContextMoveToPoint(context, rect.size.width*0.5, rect.size.width*0.5);
CGContextAddArc(context, rect.size.width*0.5, rect.size.width*0.5, rect.size.width*0.5, -0.5*M_PI, -0.5*M_PI+self.progressRate*2*M_PI, 0);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFill); //绘制路径加填充
/*官方文档参数说明
void CGContextAddArc ( CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise );
Parameters:
c-A graphics context.
x-The x-value, in user space coordinates, for the center of the arc.
y-The y-value, in user space coordinates, for the center of the arc.
radius-The radius of the arc, in user space coordinates.
startAngle-The angle to the starting point of the arc, measured in radians from the positive x-axis.
endAngle-The angle to the end point of the arc, measured in radians from the positive x-axis.
clockwise-Specify 1 to create a clockwise arc or 0 to create a counterclockwise arc
*/
}
@end
#import <UIKit/UIKit.h>
#import "test.h"
@interface MyProgressView : UIView
@property(nonatomic, strong)test *imgOutSide;
@property(nonatomic, strong)UIView *imgInner;
-(void)animatProgress:(float)progress;
-(void)addProgressToView:(UIView *)view;
@end
“`
import “MyProgressView.h”
import “common.h”
@implementation MyProgressView
-(id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
_imgInner = [self imgViewWithFrame:CGRectMake((frame.size.width-60)*0.5, (frame.size.height-60)*0.5, 60, 60)];
_imgOutSide = [[test alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
_imgOutSide.backgroundColor= [UIColor whiteColor];
//遮盖
UIView *coverView = [self imgViewWithFrame:CGRectMake(5, 5, 50, 50)];
coverView.backgroundColor = kColor(216, 216, 216);
[self.imgInner addSubview:self.imgOutSide];
[self.imgInner addSubview:coverView];
[self addSubview:self.imgInner];
}
return self;
}
-(void)addProgressToView:(UIView *)view
{
[view addSubview:self];
}
-(UIView*)imgViewWithFrame:(CGRect)frame
{
UIView *imgView = [[UIView alloc]initWithFrame:frame];
imgView.layer.cornerRadius = frame.size.width*0.5;
imgView.clipsToBounds = YES;
imgView.layer.borderColor = [UIColor grayColor].CGColor;
imgView.layer.borderWidth = 2.0f;
return imgView;
}
-(void)animatProgress:(float)progress
{
self.imgOutSide.progressRate = progress;
[self.imgOutSide setNeedsDisplay];
}
“`