进度条的自定义

自定义环形进度条

思路:

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];

}

“`这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值