//
//  GradientView.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 12.11.2020.
//

import UIKit

class GradientView: UIView {
    private let startColor:UIColor
    private let endColor:UIColor
    private let opacity:Float
    
    init(startColor:UIColor, endColor:UIColor, opacity:Float = 1.0) {
        self.startColor = startColor
        self.endColor = endColor
        self.opacity = opacity
        
        super.init(frame: .zero)
        prepare()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override class var layerClass: AnyClass {
        return CAGradientLayer.self
    }
}

//MARK:- Prepare
private extension GradientView {
    func prepare() {
        self.backgroundColor = .clear
        
        guard let gradientLayer = self.layer as? CAGradientLayer else { return }
        gradientLayer.colors = [startColor.cgColor, endColor.cgColor]
        gradientLayer.opacity = self.opacity
        gradientLayer.locations = [0.0, 1.0]
    }
}
