iOS环形进度条

[Demo下载]
(https://git.oschina.net/remainedmute/CircleProgressDemo.git)

废话不多,先上效果图

效果图.gif

![Uploading 大图浏览_874905.gif . . .]

看完效果,来看代码

1.主要原理:在drawRect方法中实现如下方法

//An opaque type that represents a Quartz 2D drawing environment.
//一个不透明类型的Quartz 2D绘画环境,相当于一个画布,你可以在上面任意绘画
CGContextRef context = UIGraphicsGetCurrentContext();
/*画圆*/

//边框圆 -- 底色
UIColor *aColor = _cusBackgroundColor;
if (!aColor) aColor = [UIColor colorWithRed:78/255.0 green:77/255.0 blue:77/255.0 alpha:1];
CGContextSetStrokeColorWithColor(context, aColor.CGColor);  //画笔线的颜色

CGContextSetLineWidth(context, lineWidth);//线的宽度
 CGContextAddArc(context, centerX, centerY, radius, 2*PI*(bgsAngle/360.0),8*PI*(bgeAngle/360.0), NO);      // 添加一个圆
CGContextDrawPath(context, kCGPathStroke);              // 绘制路径

//边框圆 -- 前景色
UIColor *bColor = _foregroundColor;
if (!bColor) bColor = [UIColor colorWithRed:27/255.0 green:158/255.0 blue:255/255.0 alpha:1];
CGContextSetStrokeColorWithColor(context, bColor.CGColor);  //画笔线的颜色

CGContextSetLineWidth(context, lineWidth);//线的宽度
//void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
// x,y为圆点坐标,radius半径,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针。

CGContextAddArc(context, centerX, centerY, radius, 2*PI*(_startAngle/360.0), 2*PI*(_endAngle/360), NO);      // 添加一个圆
CGContextDrawPath(context, kCGPathStroke);              // 绘制路径

2.设置每次的进度间隔为2,通过设置的起始角度,计算出需要循环的次数,然后不断刷新UI

- (void)doSomething {
// 获取循环次数
int count = (_animCountAngle - _startAngle)/2 + 1;
if (count <= 0) count = 1;

// 定义时间间隔
CGFloat timeSlot = _animTime * 1.0/count;     // 每一度停止时间
if (timeSlot < 0) timeSlot = 0;

// 获取角度间隔
int angleSlot = 2;

// 获取百分比间隔
CGFloat ratioSlot = 0;
if (_animRatio != nil) ratioSlot = [_animRatio floatValue]* 1.0/count;

@synchronized(self) {
    // 循环更新UI
    for (int i = 0; i <= count; i++) {
        // 设置显示的角度
        _endAngle = _startAngle + i * angleSlot;
        if (_endAngle > _animCountAngle) _endAngle = _animCountAngle;
        
        // 设置
        if (_animRatio != nil) _ratio = [NSString stringWithFormat:@"%.2f",(float)(0 + i * ratioSlot)];
        else _ratio = nil;
        
        [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
        if (self.animation) {
            [NSThread sleepForTimeInterval:timeSlot];
        }
    }
}
}

3.值得注意的是,上面的方法中用到了@synchronized关键字,主要是因为当我们将这个view放在UITableViewCell里的时候,然后对UITableView进行不断刷新会出现闪退现象,经过排查是for循环处线程问题,所以对for循环加了线程互斥锁@synchronized。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • viewController.m #import "ViewController.h" #import "TBCy...
    Willism阅读 3,957评论 0 0
  • .h文件 #import @interface YTProgressView : UIView@property...
    木火柴阅读 6,630评论 1 0
  • - (void)drawRect:(CGRect)rect{ CGContextRef context = UIG...
    _桃夭大人_阅读 4,449评论 0 1
  • 1.属性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作...
    曾令伟阅读 4,681评论 0 10
  • -能用钱解决的事情,都不是事儿;能自己动手做的事情绝不开口劳烦别人 -每个人的生活都不容易,都在背着自己的壳负重前...
    汮言阅读 871评论 0 0