Here I use create an extension for gradient
@IBDesignable
class GradientView: UIView {
@IBInspectable var firstColor: UIColor = UIColor.clear {
didSet {
updateGradientView()
}
}
@IBInspectable var secondColor: UIColor = UIColor.clear {
didSet {
updateGradientView()
}
}
@IBInspectable var thirdColor: UIColor = UIColor.clear {
didSet {
updateGradientView()
}
}
override class var layerClass: AnyClass {
get {
return CAGradientLayer.self
}
}
@IBInspectable var isHorizontal: Bool = true {
didSet {
updateGradientView()
}
}
func updateGradientView() {
let gradientLayer = self.layer as! CAGradientLayer
if (self.isHorizontal) {
//gradientLayer.colors = [firstColor, secondColor, thirdColor].map{$0.cgColor}
gradientLayer.colors = [firstColor.withAlphaComponent(1.0), secondColor.withAlphaComponent(0.59), thirdColor.withAlphaComponent(1.0)].map{$0.cgColor}
gradientLayer.locations = [0.29, 0.60, 1]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint (x: 1.0, y: 0.5)
} else {
gradientLayer.colors = [firstColor.withAlphaComponent(0.5), secondColor.withAlphaComponent(0.0), thirdColor.withAlphaComponent(0.0)].map{$0.cgColor}
gradientLayer.startPoint = CGPoint(x: 0.5, y: 1.0)
gradientLayer.endPoint = CGPoint (x: 0.5, y: 0.5)
}
}
}