Sundeep Saluja
Sundeep Saluja

Reputation: 1249

Gradient effect on some portion of UIImageView

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

Upvotes: 0

Views: 795

Answers (2)

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

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

Upvotes: 1

Taj Ahmed
Taj Ahmed

Reputation: 895

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?

Upvotes: 0

Related Questions