圆圈进度条简单绘制

项目中要用到圆圈的进度条,本来想用第三方的,后面还是自己简单的画了一个。大致是下图这个样子:

圆圈进度条.png

也是很简单的一个东西。
我这边是采用的CAShapeLayer和CGPath来的。
先来个背景虚线圆:

-(void)setBackGroundShapeLayer
{
    CAShapeLayer *shapeLayer=[[CAShapeLayer alloc]init];
    
    CGMutablePathRef path=CGPathCreateMutable();
    shapeLayer.lineWidth=20;
    shapeLayer.strokeColor=RGB(200, 200, 200, 1).CGColor;
    shapeLayer.fillColor=[UIColor clearColor].CGColor;
    CGPathAddEllipseInRect(path, nil, self.bounds);
    shapeLayer.path=path;
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:3], nil]];
    CGPathRelease(path);
    [self.layer addSublayer:shapeLayer];
    self.backShapeLayer=shapeLayer;
}

各属性的含义就自行百度了解吧。
然后就是表面动态圆:

 CAShapeLayer *shapeLayer=[[CAShapeLayer alloc]init];
    
    CGMutablePathRef path=CGPathCreateMutable();
    shapeLayer.lineWidth=20;
    shapeLayer.strokeColor=REHAU_GREEN_COLOR.CGColor;
    shapeLayer.fillColor=[UIColor clearColor].CGColor;
    shapeLayer.strokeStart=0;
    shapeLayer.strokeEnd=0;
    CGPathAddEllipseInRect(path, nil, self.bounds);
    shapeLayer.path=path;
    CGPathRelease(path);
    [self.layer addSublayer:shapeLayer];
    self.progressShapeLayer=shapeLayer;
    
    shapeLayer.transform=CATransform3DIdentity;
    shapeLayer.transform=CATransform3DRotate(shapeLayer.transform,-M_PI_2, 0.0,  0.0, 1);
    shapeLayer.frame=self.bounds;

这里是通过strokeStart和strokeEnd来表现进度的变化,但是默认的strokeStart是从水平顺时针开始的,所以我这里是把这个shapeLayer给转了一下。
中间的字是用CATextLayer:

CATextLayer *textLayer=[[CATextLayer alloc]init];
    textLayer.frame=CGRectMake(self.width/2-95, self.height/2-38, 190, 76);
    [self.layer addSublayer:textLayer];
    
    textLayer.foregroundColor=REHAU_GREEN_COLOR.CGColor;
    textLayer.alignmentMode=kCAAlignmentCenter;
    textLayer.wrapped=YES;
    
    UIFont *font=[UIFont systemFontOfSize:60];
    CFStringRef fontName = (__bridge CFStringRef)font.fontName;
    CGFontRef fontRef = CGFontCreateWithFontName(fontName);
    textLayer.font = fontRef;
    textLayer.fontSize = font.pointSize;
    CGFontRelease(fontRef);
    
    textLayer.string=@"0%";
    textLayer.contentsScale=[UIScreen mainScreen].scale;
    self.textLayer=textLayer;

CATextLayer 百度一堆怎么用的。
要改变动态圆 只用改变strokeEnd 就行了,strokeEnd要比strokeStart大。

-(void)setRatio:(CGFloat)ratio
{
   // [CATransaction begin];
    //[CATransaction setDisableActions:YES];
        
    self.progressShapeLayer.strokeEnd=ratio;
    self.textLayer.string=[NSString stringWithFormat:@"%.lf%%",ratio*100];
    //[CATransaction commit];
}

不要动画就加CATransaction。
到此一个简单的圆圈进度就做完了·可以根据自己的需求自行绘制,做一些修改。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一种新的协议。它实...
    香橙柚子阅读 24,391评论 8 183
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,650评论 4 61
  • 女强人W小姐,事业力100%,心机100%,情商100%,爱情0.马上奔三十了,自己的终身大事终究未解决,且不说家...
    刘恋少女阅读 2,713评论 0 0
  • 年年岁岁花相似,岁岁年年人不同! 又过了一年一度的中秋佳节,感叹时间的马不停蹄,也催人不停奋进。 生活是美好的!可...
    非凡说阅读 2,985评论 0 0
  • 姓名:王炳娟省份吉林 【日管理打卡第11天】 【知~学习】 1、醒来:知行合一传习读本 2、线上6访、线下5访 3...
    王炳娟阅读 1,625评论 0 0

友情链接更多精彩内容