使用CAShapeLayer和贝塞尔画进度条

参考文章http://blog.it985.com/7654.html

pragma mark---画进度条使用的

@property (nonatomic, assign) CGFloat add;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;

-(void)initWithShapeLayer{

self.shapeLayer = [CAShapeLayer layer];
self.shapeLayer.frame = CGRectMake(0, 0, 200, 200);//设置shapeLayer的尺寸和位置
self.shapeLayer.position = self.center;
self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
//起点
self.shapeLayer.strokeStart = 0;
//终点
// self.shapeLayer.strokeEnd = 0.8;
//圆:起点为0 ,终点为1,如果终点设置为0.8就是把圆分成10分,占8份

//设置线条的宽度和颜色
self.shapeLayer.lineWidth = 1.0f;
self.shapeLayer.strokeColor = [UIColor redColor].CGColor;

//创建出圆形的贝塞尔曲线
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
//让贝塞尔曲线与cashaperLayer产生联系
self.shapeLayer.path = circlePath.CGPath;
//添加并显示
[self.layer addSublayer:self.shapeLayer];

_add = 0.1;

_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(circleAnima) userInfo:self repeats:YES];
}

//利用贝塞尔曲线画进度条
-(void)circleAnima{
   if (self.shapeLayer.strokeEnd > 1 && self.shapeLayer.strokeStart < 1) {
    self.shapeLayer.strokeStart += _add;
}else if(self.shapeLayer.strokeStart == 0){
    self.shapeLayer.strokeEnd += _add;
}

if (self.shapeLayer.strokeEnd == 0) {
    self.shapeLayer.strokeStart = 0;
}

if (self.shapeLayer.strokeStart == self.shapeLayer.strokeEnd) {
    self.shapeLayer.strokeEnd = 0;
}
if (self.shapeLayer.strokeEnd > 1 && self.shapeLayer.strokeStart > 1) {
    
    self.shapeLayer.strokeStart = self.shapeLayer.strokeEnd;
    
}
}

你可能感兴趣的:(使用CAShapeLayer和贝塞尔画进度条)