代码粘贴过去不用改,运行就可以看到效果了
<img src="https://img-blog.csdn.net/20160426170919047?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />@interface ViewController ()
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, assign) CGFloat f;
@property (nonatomic, strong) UILabel *label; // 显示用
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建出CAShapeLayer
// 创建一个view 承载进度条 单纯用这个方法实现一个进度条
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
view.backgroundColor = [UIColor grayColor];
view.transform = CGAffineTransformMakeRotation((90.0f * M_PI) / 180.0f); // 旋转半圈后面self.shapeLayer.strokeStart = 0这样起点刚好在正下方
view.center = self.view.center;
[self.view addSubview: view];
// 初始化CAShapeLayer
CAShapeLayer *layer1 = [CAShapeLayer layer];
layer1.frame = CGRectMake(40, 40, 220, 220);
layer1.lineWidth = 1.0f; // 边框宽度是1;
layer1.fillColor = [UIColor lightGrayColor].CGColor;
layer1.strokeColor = [UIColor redColor].CGColor;
// 贝塞尔曲线
UIBezierPath *circlePath1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 220, 220)];
//让贝塞尔曲线与CAShapeLayer产生联系
layer1.path = circlePath1.CGPath;
//添加并显示
[view.layer addSublayer:layer1];
// 结果为一个圆形
// 第二个
self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.frame = CGRectMake(50, 50, 200, 200);//设置shapeLayer的尺寸和位置
//self.shapeLayer.position = view.center;
self.shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色为ClearColor
//设置线条的宽度和颜色
self.shapeLayer.lineWidth = 20.0f;
self.shapeLayer.strokeColor = [UIColor greenColor].CGColor;
//创建出圆形贝塞尔曲线
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
//让贝塞尔曲线与CAShapeLayer产生联系
self.shapeLayer.path = circlePath.CGPath;
//添加并显示
[view.layer addSublayer:self.shapeLayer];
// 结果为一个圆形
// 重点
// 如果先画一个半圆或者不完整的圆
self.shapeLayer.strokeStart = 0; // 开始的起点 默认的起点是在最右边 旋转半圈后可以在最下方
self.shapeLayer.strokeEnd = 0; // 这样就是个半圆 //如果 =0.75 就3/4圆
CAShapeLayer *layer2 = [CAShapeLayer layer];
layer2.frame = CGRectMake(60, 60, 180, 180);
layer2.lineWidth = 1.0f;
layer2.fillColor = [UIColor whiteColor].CGColor;
layer2.strokeColor = [UIColor redColor].CGColor;
UIBezierPath *circlePath2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 180, 180)];
//让贝塞尔曲线与CAShapeLayer产生联系
layer2.path = circlePath2.CGPath;
//添加并显示
[view.layer addSublayer:layer2];
self.label = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50, [UIScreen mainScreen].bounds.size.height / 2 -10, 100, 20)];
self.label.textAlignment = NSTextAlignmentCenter;
self.label.text = [NSString stringWithFormat:@"%.2f%%",self.f * 100];
[self.view addSubview:self.label];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 120, 100, 30)];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)buttonClick:(UIButton *)button{
button.userInteractionEnabled = NO; // 关闭交互避免快速点击重复动画
self.f += 0.1;
if (self.f > 1) {
self.f = 0;
}
[UIView animateWithDuration:1 animations:^{
self.shapeLayer.strokeEnd = self.f;
} completion:^(BOOL finished) {
button.userInteractionEnabled = YES;
self.label.text = [NSString stringWithFormat:@"%.2f%%",self.f * 100];
}];
}