Skip to content
Kevin Leong edited this page Dec 21, 2016 · 4 revisions

CALayer

Gradient Layer

class MyView: UIView {
    override func draw(_ rect: CGRect) {
        let gradientLayer = CAGradientLayer()

        gradientLayer.frame = layer.bounds
        gradientLayer.colors = [UIColor.red.cgColor, UIColor.green.cgColor, UIColor.yellow.cgColor]

        /**
         Defaults:
            start point x: 0.5, y: 0.0
            end point x: 0.5, y: 1.0

         Results in a vertical line segment, which the gradient always goes
         perpendicular to.

         Default gradient, therefore, goes from top to bottom.

         To achieve a left to right gradient, set the start and end points
         along a horizontal line, where the start point is to the left
         of the end point.
        */
        gradientLayer.startPoint = CGPoint(x: 0, y: 1)
        gradientLayer.endPoint = CGPoint(x: 1, y: 1)

        layer.addSublayer(gradientLayer)
    }
}

Replicator Layer

References

References

Clone this wiki locally