0

I have a requirements of adding gradient over some portion (only in left and right side ) of UIImageView. I know, this can be done by using CAGradientLayer(). But I am confused with properties locations and colors.

Below is my code. It is adding gradient at top and bottom.

 if let containerView = imageView.superView {
        let gradient = CAGradientLayer(layer: containerView.layer)
        gradient.frame = containerView.bounds
        gradient.colors = [UIColor.clear.cgColor, UIColor.blue.cgColor ,UIColor.blue.cgColor, UIColor.clear.cgColor]
        gradient.locations = [0.0 ,  0.25 , 0.85 , 1.0]
        containerView.layer.mask = gradient
    }

Thanks In Advance

Sundeep Saluja
  • 1,089
  • 2
  • 14
  • 36

2 Answers2

1

You need to create CAGradientLayer layer like this and in Any view

let gradientLayer = CAGradientLayer()
gradientLayer.frame = CGRectMake(0, 0,300, 380)
gradientLayer.colors = [UIColor(red:55.0/255, green:149.0/255, blue:227.0/255, alpha:1.0).CGColor,UIColor(red:20.0/255, green:77.0/255, blue:120.0/255, alpha:1.0).CGColor]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
imgView.layer.insertSublayer(gradientLayer, atIndex: 0)

For All direction like top-buttom,left-right and vice versa just you need to change startPoint And endPoint Output of above code is

enter image description here

jignesh Vadadoriya
  • 3,244
  • 3
  • 18
  • 29
0

gradient.colors

gradient.colorsproperty is used to define the color of each gradient

If you see the documentation,

gradient.locations is used to define gradient stop points between your colors

Documentation: An optional array of NSNumber objects defining the location of each gradient stop as a value in the range [0,1]

is this you are looking for?

Community
  • 1
  • 1
Taj Ahmed
  • 895
  • 11
  • 19