iOS 自定义控件 progressView(环形进度条)

之前做项目的时候有用到环形进度条,先是在网上找了一下第三方控件,发现好用是好用,就是东西太多了,有点复杂,还不如自己写一个简单点适合自己用的。

先把自定义控件的效果图贴出来。

      

其实我写的这个控件很简单。索性就直接把源码贴出来吧。

.h文件的内容就是一些声明

#import <UIKit/UIKit.h>


@interface ProgressView : UIView


//中心颜色

@property (strongnonatomic)UIColor *centerColor;

//圆环背景色

@property (strongnonatomic)UIColor *arcBackColor;

//圆环色

@property (strongnonatomic)UIColor *arcFinishColor;

@property (strongnonatomic)UIColor *arcUnfinishColor;



//百分比数值(0-1

@property (assignnonatomic)float percent;


//圆环宽度

@property (assignnonatomic)float width;


@end




.m文件里就是具体实现了


#import "ProgressView.h"


@implementation ProgressView


- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = ClearColor;

        _percent = 0;

        _width = 0;

    }

    

    return self;

}


- (void)setPercent:(float)percent{

    _percent = percent;

    [self setNeedsDisplay];

}


- (void)drawRect:(CGRect)rect{

    [self addArcBackColor];

    [self drawArc];

    [self addCenterBack];

    [self addCenterLabel];

}


- (void)addArcBackColor{

    CGColorRef color = (_arcBackColor == nil) ? [UIColorlightGrayColor].CGColor : _arcBackColor.CGColor;


    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    CGSize viewSize = self.bounds.size;

    CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);

    

    // Draw the slices.

    CGFloat radius = viewSize.width / 2;

    CGContextBeginPath(contextRef);

    CGContextMoveToPoint(contextRef, center.x, center.y);

    CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI0);

    CGContextSetFillColorWithColor(contextRef, color);

    CGContextFillPath(contextRef);

}


- (void)drawArc{

    if (_percent == 0 || _percent > 1) {

        return;

    }

    

    if (_percent == 1) {

        CGColorRef color = (_arcFinishColor == nil) ? [UIColorgreenColor].CGColor : _arcFinishColor.CGColor;

        

        CGContextRef contextRef = UIGraphicsGetCurrentContext();

        CGSize viewSize = self.bounds.size;

        CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);

        // Draw the slices.

        CGFloat radius = viewSize.width / 2;

        CGContextBeginPath(contextRef);

        CGContextMoveToPoint(contextRef, center.x, center.y);

        CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI0);

        CGContextSetFillColorWithColor(contextRef, color);

        CGContextFillPath(contextRef);

    }else{

        

        float endAngle = 2*M_PI*_percent;

        

        CGColorRef color = (_arcUnfinishColor == nil) ? [UIColorblueColor].CGColor : _arcUnfinishColor.CGColor;

        CGContextRef contextRef = UIGraphicsGetCurrentContext();

        CGSize viewSize = self.bounds.size;

        CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);

        // Draw the slices.

        CGFloat radius = viewSize.width / 2;

        CGContextBeginPath(contextRef);

        CGContextMoveToPoint(contextRef, center.x, center.y);

        CGContextAddArc(contextRef, center.x, center.y, radius,0,endAngle, 0);

        CGContextSetFillColorWithColor(contextRef, color);

        CGContextFillPath(contextRef);

    }

    

}


-(void)addCenterBack{

    float width = (_width == 0) ? 5 : _width;

    

    CGColorRef color = (_centerColor == nil) ? [UIColorwhiteColor].CGColor : _centerColor.CGColor;

    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    CGSize viewSize = self.bounds.size;

    CGPoint center = CGPointMake(viewSize.width / 2, viewSize.height / 2);

    // Draw the slices.

    CGFloat radius = viewSize.width / 2 - width;

    CGContextBeginPath(contextRef);

    CGContextMoveToPoint(contextRef, center.x, center.y);

    CGContextAddArc(contextRef, center.x, center.y, radius,0,2*M_PI0);

    CGContextSetFillColorWithColor(contextRef, color);

    CGContextFillPath(contextRef);

}


- (void)addCenterLabel{

    NSString *percent = @"";


    float fontSize = 14;

    UIColor *arcColor = [UIColor blueColor];

    if (_percent == 1) {

        percent = @"100%";

        fontSize = 14;

        arcColor = (_arcFinishColor == nil) ? [UIColorgreenColor] : _arcFinishColor;

        

    }else if(_percent < 1 && _percent >= 0){

        

        fontSize = 13;

        arcColor = (_arcUnfinishColor == nil) ? [UIColorblueColor] : _arcUnfinishColor;

        percent = [NSStringstringWithFormat:@"%0.2f%%",_percent*100];

    }

    

    CGSize viewSize = self.bounds.size;

    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle allocinit];

    paragraph.alignment = NSTextAlignmentCenter;

    NSDictionary *attributes = [NSDictionarydictionaryWithObjectsAndKeys:[UIFontboldSystemFontOfSize:fontSize],NSFontAttributeName,arcColor,NSForegroundColorAttributeName,[UIColorclearColor],NSBackgroundColorAttributeName,paragraph,NSParagraphStyleAttributeName,nil];


    [percent drawInRect:CGRectMake(5, (viewSize.height-fontSize)/2, viewSize.width-10, fontSize)withAttributes:attributes];

}


@end



具体的调用就是

  ProgressView *progress = [[ProgressViewalloc]initWithFrame:CGRectMake(detil.width-65106060)];

    progress.arcFinishColor = COLOR_STRING(@"#75AB33");

    progress.arcUnfinishColor = COLOR_STRING(@"#0D6FAE");

    progress.arcBackColor = COLOR_STRING(@"#EAEAEA");

    progress.percent = 1;

    [detil addSubview:progress];


当然这些都有默认值,也可以不设置。

当然这么简单肯定会有缺陷,所以如果你有发现什么问题,或更好地方法可以提出来大家一起研究。

最后,如果你不想粘贴代码,那么源码我也已经上传到资源。

progressView 资源: 空间传送门
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值