//
//  GSPregressView.swift
//  TestDIYProgressView
//
//  Created by Mazy on 2018/9/4.
//  Copyright © 2018年 mazy. All rights reserved.
//

import UIKit

class GSPregressView: UIView {

    @IBInspectable public var strokeColor: UIColor = UIColor(red: 39/255.0, green: 197/255.0, blue: 158/255.0, alpha: 0.1) {
        didSet {
//            self.colorMaskLayer.strokeColor = strokeColor.cgColor
            self.backgroundColor = massColor.withAlphaComponent(0.15)
        }
    }
    
    @IBInspectable public var strokeStart: CGFloat = 1 {
        didSet {
            self.colorMaskLayer.frame = CGRect.init(x: 0, y: 0, width: self.frame.size.width*strokeStart, height: self.frame.size.height)
        }
    }
    
    @objc var colorMaskLayer = CAGradientLayer()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupColorMaskLayer()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupColorMaskLayer()
    }
    
    /// 设置遮挡图层
    fileprivate func generateMaskLayer() -> CAGradientLayer {
        let maskLayer = CAGradientLayer()
        maskLayer.frame = CGRect.zero
        maskLayer.colors = [massColor.cgColor, massColor.cgColor]
        maskLayer.startPoint = CGPoint.init(x: 0, y: 0)
        maskLayer.endPoint = CGPoint.init(x: 1, y: 0.5)
        maskLayer.cornerRadius = self.layer.cornerRadius;
        return maskLayer
    }
    /// 设置颜色遮挡图层
    fileprivate func setupColorMaskLayer() {
        colorMaskLayer = generateMaskLayer()
//        self.layer.addSublayer(colorMaskLayer)
        layer.insertSublayer(colorMaskLayer, at: 0)
    }
}
