美文网首页
设计一个进度条

设计一个进度条

作者: biyuhuaping | 来源:发表于2017-08-31 17:39 被阅读17次
    1. 自定义一个UIView的子类
    //提供一个成员属性,接收下载进度值
     @property (nonatomic, assign) CGFloat progress;
    
    1. 重写成员属性progress的setter
    //每次改变成员属性progress的值,就会调用它的setter
     - (void)setProgress:(CGFloat)progress
     {
       _progress = progress;
       //当下载进度改变时,手动调用重绘方法
       [self setNeedsDisplay];
     }
    
    1. 重写(核心)
     - (void)drawRect:(CGRect)rect
     {
       //设置圆弧的半径
       CGFloat radius = rect.size.width * 0.5;
       //设置圆弧的圆心
       CGPoint center = CGPointMake(radius, radius);
       //设置圆弧的开始的角度(弧度制)
       CGFloat startAngle = - M_PI_2;
       //设置圆弧的终止角度
       CGFloat endAngle = - M_PI_2 + 2 * M_PI * self.progress;
       //使用UIBezierPath类绘制圆弧
       UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius - 5 startAngle:startAngle endAngle:endAngle clockwise:YES];
       //将绘制的圆弧渲染到图层上(即显示出来)
       [path stroke];
     }
    

    相关文章

      网友评论

          本文标题:设计一个进度条

          本文链接:https://www.haomeiwen.com/subject/qlxcjxtx.html