iOS 通过UIBezierPath和CAShapeLayer绘制圆形进度条

不需要重写drawRect方法绘制圆形进度条,希望对大家有帮助

    //创建显示进度label
    _progressLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 100)];
    _progressLabel.font = [UIFont systemFontOfSize:30.0];
    _progressLabel.textAlignment = NSTextAlignmentCenter;
    _progressLabel.center = self.view.center;
    [self.view addSubview:_progressLabel];

    //创建圆形进度条
    [self createBezierPath:CGRectMake(0, 0, 150, 150)];
#pragma mark - 绘制圆形进度条
-(void)createBezierPath:(CGRect)mybound
{
    //外圆
    _trackPath = [UIBezierPath bezierPathWithArcCenter:self.view.center//设置圆心坐标
                                                radius:(mybound.size.width - 0.7)/ 2//设置圆的半径
                                            startAngle:0//设置画圆弧度起始角度
                                              endAngle:M_PI * 2//设置画圆弧度结束角度
                                             clockwise:YES];//是否顺时针画圆
    _trackLayer = [CAShapeLayer new];
    [self.view.layer addSublayer:_trackLayer];
    _trackLayer.fillColor = nil;
    _trackLayer.strokeColor=[UIColor grayColor].CGColor;
    _trackLayer.path = _trackPath.CGPath;
    _trackLayer.lineWidth=2;
    _trackLayer.frame = mybound;

    _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(startProgressAction:) userInfo:@{@"rect":NSStringFromCGRect(mybound)} repeats:YES];
}

-(void)startProgressAction:(NSTimer *)timer
{
    static CGFloat hudu = 0;
    CGRect mybound = CGRectFromString([[timer userInfo] objectForKey:@"rect"]);
    //内圆
    if (!_progressPath) {
        [_progressPath removeAllPoints];
        _progressPath = nil;
    }
    _progressPath = [UIBezierPath bezierPathWithArcCenter:self.view.center
                                                   radius:(mybound.size.width - 0.7)/ 2
                                               startAngle:- M_PI_2
                                                 endAngle:(M_PI * 2) * hudu - M_PI_2
                                                clockwise:YES];
    _progressLayer = [CAShapeLayer new];
    [self.view.layer addSublayer:_progressLayer];

    _progressLayer.fillColor    = nil;
    _progressLayer.strokeColor  = [UIColor redColor].CGColor;
    _progressLayer.lineCap      = kCALineCapRound;
    _progressLayer.path         = _progressPath.CGPath;
    _progressLayer.lineWidth    = 2;
    _progressLayer.frame        = mybound;
    _progressLabel.text         = [NSString stringWithFormat:@"%.2f%@",hudu,@"%"];

    if (hudu >= 1.000000) {
        _progressLabel.text = @"100%";
        [_timer invalidate];
        _timer = nil;
        hudu = 0;
    }
    hudu += 0.01;
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值