Gradient effect on some portion of UIImageView

笑着哭i 提交于 2019-12-13 07:23:00

问题


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


回答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




回答2:


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?



来源:https://stackoverflow.com/questions/41013702/gradient-effect-on-some-portion-of-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!