自制进度圈

最终要实现的效果,如图:


将实现这个效果的View写成了分类:

.h文件:

#import <UIKit/UIKit.h>

 

@interface CZProgressView : UIView

 

@property(nonatomic,assign) CGFloat progress;

 

@end

 

.m文件:

#import "CZProgressView.h"

 

@interface CZProgressView ()

 

@property(nonatomic,strong) UILabel *label;

 

@end

 

@implementation CZProgressView

 

-(UILabel *)label{

    if(_label == nil){

       _label = [[UILabelalloc] initWithFrame:self.bounds];

        _label.textAlignment = NSTextAlignmentCenter;

       [self addSubview:_label];

    }

    return _label;

}

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColor clearColor];

      

    }

    return self;

}

 

-(void)setProgress:(CGFloat)progress{

    _progress = progress;

    self.label.text = [NSStringstringWithFormat:@"%0.2f%%",progress* 100];

    [self setNeedsDisplay];

}

 

- (void)drawRect:(CGRect)rect

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    

    CGFloat w = rect.size.width * 0.5;

    CGFloat h = rect.size.height * 0.5;

    

    CGPoint centerPoint = CGPointMake(w,h);

    CGFloat radius = w > h ? h : w;

    radius-= 5;

    CGFloat startAngle = - M_PI_2;

    CGFloat endAngle = self.progress * 2 * M_PI + startAngle;

    

    [path addArcWithCenter:centerPoint radius:radius startAngle:startAngleendAngle:endAngle clockwise:YES];

    

    path.lineWidth = 10.0;

    path.lineCapStyle = kCGLineCapRound;

    [[UIColor yellowColor] set];

 

    [path stroke];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值