I am trying to provide gradient layer to the View on created XIB. I have to give gradient colors with combination of 5 colors. But unfortunately gradient is not working.
These are colors array:-
let colors = [UIColor(red: 255.0, green: 252.0, blue: 252.0, alpha: 0).cgColor, UIColor(red: 126, green: 125, blue: 125, alpha: 1).cgColor, UIColor(red: 197.0, green: 195.0, blue: 195.0, alpha: 0.62).cgColor, UIColor(red: 126.0, green: 125.0, blue: 125.0, alpha: 1).cgColor, UIColor(red: 189.0, green: 189.0, blue: 189.0, alpha: 0.7).cgColor, UIColor(red: 255.0, green: 255.0, blue: 255.0, alpha: 0.0).cgColor]
Here i am applying gradient:-
self.separatorView.applyGradient(colors: colors,
locations: [0.0, 1.0],
direction: .leftToRight)
func applyGradient(colors: [Any]?, locations: [NSNumber]? = [0.0, 0.0], direction: Direction = .leftToRight) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.bounds
gradientLayer.colors = colors
//gradientLayer.locations = [0.0, 0.8, 1.0]
switch direction {
case .topToBottom:
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
case .bottomToTop:
gradientLayer.startPoint = CGPoint(x: 0.5, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 0.0)
case .leftToRight:
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
case .rightToLeft:
gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.5)
}
self.layer.addSublayer(gradientLayer)
}