ios 环形进度条

viewController.m


#import "ViewController.h"

#import "TBCycleView.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet TBCycleView *cycleView;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (IBAction)slider:(UISlider *)sender {

self.cycleView.label.text = [NSString stringWithFormat:@"%.2f%%", sender.value*100];

[self.cycleView drawProgress:sender.value];

}

@end


TBCycleView.h


#import@interface TBCycleView : UIView

@property (nonatomic, strong) UILabel *label;

- (void)drawProgress:(CGFloat )progress;

@end



TBCycleView.m


#import "TBCycleView.h"

@interface TBCycleView ()

@property (nonatomic, assign) CGFloat progress;

@property (nonatomic, strong) CAShapeLayer *progressLayer;

@end

@implementation TBCycleView

- (UILabel *)label

{

if (_label == nil) {

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

label.textAlignment = NSTextAlignmentCenter;

label.center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);

[self addSubview:label];

_label = label;

}

return _label;

}

- (void)drawProgress:(CGFloat )progress

{

_progress = progress;

_progressLayer.opacity = 0;

[self setNeedsDisplay];

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

[self drawCycleProgress];

}

- (void)drawCycleProgress

{

CGPoint center = CGPointMake(100, 100);

CGFloat radius = 90;

CGFloat startA = - M_PI_2;  //设置进度条起点位置

CGFloat endA = -M_PI_2 + M_PI * 2 * _progress;  //设置进度条终点位置

//获取环形路径(画一个圆形,填充色透明,设置线框宽度为10,这样就获得了一个环形)

_progressLayer = [CAShapeLayer layer];//创建一个track shape layer

_progressLayer.frame = self.bounds;

_progressLayer.fillColor = [[UIColor clearColor] CGColor];  //填充色为无色

_progressLayer.strokeColor = [[UIColor redColor] CGColor]; //指定path的渲染颜色,这里可以设置任意不透明颜色

_progressLayer.opacity = 1; //背景颜色的透明度

_progressLayer.lineCap = kCALineCapRound;//指定线的边缘是圆的

_progressLayer.lineWidth = 10;//线的宽度

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];//上面说明过了用来构建圆形

_progressLayer.path =[path CGPath]; //把path传递給layer,然后layer会处理相应的渲染,整个逻辑和CoreGraph是一致的。

[self.layer addSublayer:_progressLayer];

//生成渐变色

CALayer *gradientLayer = [CALayer layer];

//左侧渐变色

CAGradientLayer *leftLayer = [CAGradientLayer layer];

leftLayer.frame = CGRectMake(0, 0, self.bounds.size.width / 2, self.bounds.size.height);    // 分段设置渐变色

leftLayer.locations = @[@0.3, @0.9, @1];

leftLayer.colors = @[(id)[UIColor yellowColor].CGColor, (id)[UIColor greenColor].CGColor];

[gradientLayer addSublayer:leftLayer];

//右侧渐变色

CAGradientLayer *rightLayer = [CAGradientLayer layer];

rightLayer.frame = CGRectMake(self.bounds.size.width / 2, 0, self.bounds.size.width / 2, self.bounds.size.height);

rightLayer.locations = @[@0.3, @0.9, @1];

rightLayer.colors = @[(id)[UIColor yellowColor].CGColor, (id)[UIColor redColor].CGColor];

[gradientLayer addSublayer:rightLayer];

[gradientLayer setMask:_progressLayer]; //用progressLayer来截取渐变层

[self.layer addSublayer:gradientLayer];

}

@end


ios 环形进度条_第1张图片

你可能感兴趣的:(ios 环形进度条)