//
//  LineProcessView.m
//  Open
//
//  Created by 雷俊博 on 2020/7/14.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "LineProcessView.h"
#define Radius 4
@implementation LineProcessView{
    
    CAShapeLayer* _layer;
    
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        CGRect frame = self.bounds;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.colors = @[(__bridge id)[UIColor colorWithHex:0x15bdef].CGColor, (__bridge id)[UIColor colorWithHex:0x22aafa].CGColor];
        gradientLayer.locations = @[@0.0, @1.0];
        gradientLayer.startPoint = CGPointMake(1.0, 0);
        gradientLayer.endPoint = CGPointMake(1.0, 1.0);
        gradientLayer.frame = frame;
        [self.layer addSublayer:gradientLayer];
        [self setupView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        CGRect frame = self.bounds;
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.colors = @[(__bridge id)[UIColor colorWithHex:0x15bdef].CGColor, (__bridge id)[UIColor colorWithHex:0x22aafa].CGColor];
        gradientLayer.locations = @[@0.0, @1.0];
        gradientLayer.startPoint = CGPointMake(1.0, 0);
        gradientLayer.endPoint = CGPointMake(1.0, 1.0);
        gradientLayer.frame = frame;
        [self.layer addSublayer:gradientLayer];
        [self setupView];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    CGRect frame = self.bounds;
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHex:0x15bdef].CGColor, (__bridge id)[UIColor colorWithHex:0x22aafa].CGColor];
    gradientLayer.locations = @[@0.0, @1.0];
    gradientLayer.startPoint = CGPointMake(1.0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 1.0);
    gradientLayer.frame = frame;
    [self.layer addSublayer:gradientLayer];
    [self setupView];
}

-(void)setupView{
    [self setupCornerRadius];
    CGFloat width =self.bounds.size.width;
    
    CGFloat height = self.bounds.size.height;
    
    UIBezierPath * bezierPath = [[UIBezierPath alloc]init];
    
    [bezierPath moveToPoint:CGPointMake(Radius, height)];
    
    [bezierPath addLineToPoint:CGPointMake(Radius, 0)];
    
    _layer = [CAShapeLayer layer];
    
    _layer.path= bezierPath.CGPath;
    
    _layer.fillColor = [UIColor clearColor].CGColor;
    
    _layer.strokeEnd = 0;
    
    _layer.strokeColor = [UIColor colorWithHex:0x00ffff].CGColor;
    
    _layer.lineWidth = width;//线宽
    
    _layer.lineCap = @"round";//圆角
    
    [self.layer addSublayer:_layer];
    
    //self.layer.cornerRadius=  width/2;
    
    //self.clipsToBounds = YES;
    
    self.backgroundColor = RGBACOLOR(216, 216, 216, 1);
}


- (void)setupCornerRadius
{
    CGFloat width =self.bounds.size.width;
    
    CGFloat height = self.bounds.size.height;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, width, height)
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight
                                                         cornerRadii:CGSizeMake(Radius, Radius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

-(void)setProcessValue:(float)processValue{
    
    _processValue= processValue;
    
    _layer.strokeEnd= processValue;
    
}

-(void)setFillColor:(UIColor*)fillColor{
    
    _fillColor= fillColor;
    
    _layer.strokeColor = _fillColor.CGColor;
    
}

-(void)setDefaultColor:(UIColor*)defaultColor{
    
    _defaultColor= defaultColor;
    
    self.backgroundColor= defaultColor;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
