//
//  LXCircle.m
//  LXBezierPath
//
//  Created by zhongzhi on 2017/7/21.
//  Copyright © 2017年 漫漫. All rights reserved.
//

#import "LXCircle.h"
#import "UIColor+HExtension.h"

#define LBColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
@interface LXCircle(){
    CAGradientLayer *gradientLayer;
}

@property(nonatomic,assign)CGFloat lineWidth;

@end

@implementation LXCircle

-(instancetype)initWithFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth{
    self = [super initWithFrame:frame];
    if (self) {
        
        _lineWidth = lineWidth;
    
        [self seup:frame];
    }
    return self;
}

-(void)seup:(CGRect)rect{
    
    CAShapeLayer *shapeLayer =[[CAShapeLayer alloc] init];

    shapeLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);;
    shapeLayer.lineWidth = _lineWidth;

    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = kMassColorAlpha(0.4).CGColor;

    UIBezierPath *bezierPath =[UIBezierPath bezierPathWithRoundedRect:shapeLayer.frame cornerRadius:self.bounds.size.height/2];
    shapeLayer.path = bezierPath.CGPath;

    [self.layer addSublayer:shapeLayer];

    //渐变色，加蒙版，显示的蒙版的区域
    gradientLayer =[[CAGradientLayer alloc]init];
    
    gradientLayer.frame = CGRectMake(_lineWidth+1, _lineWidth+1, self.bounds.size.width-2*(_lineWidth+1), self.bounds.size.height-2*(_lineWidth+1));
    
    gradientLayer.colors = @[(id)kColorWithRGB(164, 246, 253).CGColor, (id)kMassColorAlpha(1).CGColor];
    
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0.5);
    gradientLayer.cornerRadius = (self.bounds.size.height-2*(_lineWidth+1))/2;
    
    [self.layer addSublayer:gradientLayer];
}

-(void)setProgress:(CGFloat)progress{
    _progress = progress;

    gradientLayer.frame = CGRectMake(_lineWidth+1, _lineWidth+1, (self.bounds.size.width-2*(_lineWidth+1))*_progress, self.bounds.size.height-2*(_lineWidth+1));
}

@end
