Yoloabdo
4/1/2018 - 9:01 PM

Gradient view for UIView - Swift 4.1

Gradient view for UIView - Swift 4.1


//
//  GradientView.swift
//
//  Created by abdelrahman mohamed on 3/19/18.
//  Copyright © 2018 abdelrahman mohamed. All rights reserved.
//
import UIKit

@IBDesignable public class GradientView: UIView {
    
    @IBInspectable var firstColor: UIColor!{
        didSet{
            configureGradientLayer()
        }
    }
    @IBInspectable var secondColor: UIColor!{
        didSet{
            configureGradientLayer()
        }
    }
    
    override open class var layerClass: AnyClass {
        return CAGradientLayer.classForCoder()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configureGradientLayer()
    }
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        configureGradientLayer()
    }
    
    
    func configureGradientLayer() {
        let gradientLayer = layer as! CAGradientLayer
        gradientLayer.colors = [secondColor?.cgColor ?? UIColor.deepLilac.cgColor, firstColor?.cgColor ?? UIColor.blueberry.cgColor]
        gradientLayer.startPoint = CGPoint(x: 0.5, y: 0)
        gradientLayer.endPoint = CGPoint(x: 0.5, y: 1)
    }
}