How can i achieve this type of gradient in each cell?

时间:2016-12-09 13:00:34

标签: ios uiimageview tableviewcell cagradientlayer

enter image description here

How can i achieve the gradient below each cell ?

2 个答案:

答案 0 :(得分:0)

You can try with following code:

class func addGradientInView(_ view : UIImageView , Color1 : UIColor , color2 : UIColor , cornerRadius : CGFloat, width : CGFloat) -> CAGradientLayer
    {
        view.layer.cornerRadius = cornerRadius
        view.layer.masksToBounds = true
        view.layer.layoutSublayers()
        let labelGradient: CAGradientLayer = CAGradientLayer()
        labelGradient.masksToBounds = true
        labelGradient.frame = view.bounds
        labelGradient.frame.size.width = width
        labelGradient.frame.size.height = width
        labelGradient.colors = [(Color1.cgColor as AnyObject), (color2.cgColor as AnyObject)]
        view.layer.insertSublayer(labelGradient, at: 0)
        return labelGradient
    }

To call the above method:

addGradientInView(icon_Veges, Color1: darkGray_MaxAlpha,
                                                 color2:darkGray_MinAlpha,
                                                 cornerRadius: 0.0,
                                                 width:width)

Suppose:  let darkGray_MaxAlpha = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.1)
          let darkGray_MinAlpha = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.6)

Note: Put view, color 1, color 2, width according to your need.

答案 1 :(得分:0)

创建一个UIView并查看您想要的项目,如名称,地点等。

然后初始化CAGradientLayer的对象并设置颜色,位置和框架。然后将对象子层化为UIView。

以下是我建议的代码段。

let gradient = CAGradientLayer()
    gradient.colors = [UIColor.blackColor().colorWithAlphaComponent(0).CGColor,UIColor.blackColor().colorWithAlphaComponent(0.3).CGColor]
    gradient.locations = [0.1,1.0]
    gradient.frame = gradientView.bounds
    gradientView.layer.addSublayer(gradient)