Looking for ios Answers? Try Ask4KnowledgeBase
Looking for ios Keywords? Try Ask4Keywords

iOS Creating a CGGradientLayer with multiple colors.


// View to hold the CAGradientLayer.
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
// Initialize gradient layer.
let gradientLayer: CAGradientLayer = CAGradientLayer()
// Set frame of gradient layer.
gradientLayer.frame = view.bounds
// Color at the top of the gradient.
let topColor: CGColor = UIColor.blue.cgColor
// Color at the middle of the gradient.
let middleColor: CGColor = UIColor.yellow.cgColor
// Color at the bottom of the gradient.
let bottomColor: CGColor = UIColor.green.cgColor
// Set colors.
gradientLayer.colors = [topColor, middleColor, bottomColor]
// Set locations of the colors.
gradientLayer.locations = [0.0, 0.5, 1.0]
// Insert gradient layer into view's layer heirarchy.
view.layer.insertSublayer(gradientLayer, at: 0)

Result :

Complex CAGradientLayer.