美文网首页animateiOS Developer
iOS 几种纯色圆进度条画法

iOS 几种纯色圆进度条画法

作者: _Waiting_ | 来源:发表于2017-03-10 09:41 被阅读332次

1.绘图

创建一个进度条View在.h中声明如下:

#import@interface CircleProgress : UIView

{

CGContextRef ctx;

}

@property (nonatomic, assign) float lineWidth;

@property (nonatomic, strong) UIColor *lineColor;

@property (nonatomic, assign) float startAngle;

@property (nonatomic, assign) float endAngle;

@end


.m实现如下

- (void)drawRect:(CGRect)rect {

// Drawing code

if (_startAngle == 0)

{

_startAngle = 0;

}

if (_endAngle == 0)

{

_endAngle = 360.0f;

}

if (_lineWidth == 0)

{

_lineWidth = 3;

}

if (_lineColor == nil)

{

_lineColor = [UIColor blackColor];

}

ctx = UIGraphicsGetCurrentContext();

CGContextBeginPath(ctx);

[_lineColor set];// 两种设置颜色的方式都可以

CGContextSetLineWidth(ctx, _lineWidth); // 线的宽度

CGContextSetLineCap(ctx, kCGLineCapRound);

//图形上下文,x,y为圆点坐标,半径  startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针

CGContextAddArc(ctx, self.frame.size.width/2, self.frame.size.height/2, self.frame.size.width/2 - _lineWidth , _startAngle / (180/M_PI),_endAngle /(180/M_PI), 0);

CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);

CGContextStrokePath(ctx);

CGContextAddArc(ctx,self.frame.size.width/2, self.frame.size.height/2, 10, 0, 2 * M_PI, 1);

CGContextSetFillColorWithColor(ctx, _lineColor.CGColor);

CGContextFillPath(ctx);

}

-(void)setEndAngle:(float)endAngle

{

_endAngle = endAngle;

[self setNeedsDisplay];

}

-(void)setStartAngle:(float)startAngle

{

_startAngle = startAngle;

[self setNeedsDisplay];

}

-(void)setLineWidth:(float)lineWidth

{

_lineWidth = lineWidth ;

[self setNeedsDisplay];

}

-(void)setLineColor:(UIColor *)lineColor

{

_lineColor = lineColor;

[self setNeedsDisplay];

}


具体实现如下:

_circleProgressView = [[CircleProgress alloc] initWithFrame:CGRectMake(40, 30, 240, 240)];

_circleProgressView.backgroundColor = [UIColor redColor];

_circleProgressView.lineWidth = 25;

_circleProgressView.lineColor = [UIColor blackColor];

_circleProgressView.startAngle = 0;

_circleProgressView.endAngle = 360;

[self.view addSubview:_circleProgressView];

第一种方法完工。


2.换汤不换药的再来一种  用UIBezierPath来实现

- (void)drawRect:(CGRect)rect {

// Drawing code

CGContextRef ref =  UIGraphicsGetCurrentContext();

CGContextBeginPath(ref);

[[UIColor blueColor] set];

CGContextSetLineWidth(ref, 10);

CGContextSetLineCap(ref, kCGLineCapRound);

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:self.frame.size.width/2 - 10 startAngle:0 endAngle:_endAngle/180.0 *M_PI clockwise:YES];

CGContextAddPath(ref, path.CGPath);

CGContextStrokePath(ref);

}

- (void)setEndAngle:(CGFloat)endAngle

{

_endAngle = endAngle;

[self setNeedsDisplay];

}





3.用layer来完成

用layer来完成相应的纯色进度条比较方便快捷。

//创建CAShapeLayer

CAShapeLayer *layer = [CAShapeLayer layer];

layer.frame = CGRectMake(0, 0, 200, 200);

//创建路径

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.center radius:self.frame.size.width/2 - 10 startAngle:0 endAngle:M_PI clockwise:YES];

layer.path = path.CGPath;

layer.lineWidth = 9;

layer.fillColor=[UIColor clearColor].CGColor;

layer.strokeColor=[UIColor colorWithRed:0.76f green:0.89f blue:0.89f alpha:1.00f].CGColor;

layer.lineCap = kCALineCapRound;

layer.lineJoin = kCALineJoinRound;

[sel.view.layer addSublayer:layer];


相关文章

  • iOS 几种纯色圆进度条画法

    1.绘图 创建一个进度条View在.h中声明如下: #import@interface CircleProgres...

  • iOS 自定义进度条

    一般成长值类型的进度条都是纯色的,比如这样的: 纯色的进度条无论用layer还是view,只要计算好比例就是很好做...

  • iOS 各种圆形进度条

    iOS 各种圆形进度条:UAProgressView iOS手把手教你实现圆形进度条

  • Android自定义环形进度条

    手把手教你实现一个炫酷的环形进度条。 支持纯色/渐变,可选线条样式,动画时长。 可实现扇形进度条、多个进度叠加、最...

  • 进度条

    支持属性: - 1.设置进度条类型(圆--矩形) 2.设置进度条颜色 3.设置圆的背景颜色 4.设...

  • 圆的画法

    圆的画法有两种,下面一一为大家介绍。 【方法一:切角法】 1、画一个正方形; 2、找到四条边的中间点并连接,画出一...

  • 类图与类之间关系

    UML类图画法及其之间的几种关系 最近做重构项目,需要画一下类图,发现类图的画法及其之间的几种关系已经淡忘了很多,...

  • UIBezierPath与CAShapeLayer混合使用

    一、使用CAShapeLayer绘制一个圆 二、给圆加圆形进度条动画 这时我们会发现进度条的起始点不是在顶端,把c...

  • 实现tableViewCell高度自适应(内嵌UITextVei

    iOS进度条圆形 水平 竖直带动画

  • iOS仿Windows的进度条

    标签(空格分隔): progress windows 进度条 iOS 最近喜欢上了windows上的进度条,蛮有感...

网友评论

    本文标题:iOS 几种纯色圆进度条画法

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