     }
     
     override func layoutSubviews() {
-        self.gradientLayer = self.layer as! CAGradientLayer
+        self.gradientLayer = self.layer as? CAGradientLayer
         self.gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
         self.gradientLayer.startPoint = CGPoint(x: startPointX, y: startPointY)
         self.gradientLayer.endPoint = CGPoint(x: endPointX, y: endPointY)

import UIKit

@IBDesignable class GradientView: UIView {
   
    private var gradientLayer: CAGradientLayer!
    
    @IBInspectable var topColor: UIColor = .red {

        animation.toValue = toColors
        animation.duration = duration
        animation.isRemovedOnCompletion = true
        animation.fillMode = .forwards
        animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
        self.gradientLayer?.add(animation, forKey:"animateGradient")
    }
}
