创建一个前导/尾随渐变边缘的 UIView

Create a UIView with leading / trailing faded edges

我有一个 UIView 我也想应用淡入淡出/渐变。我希望它只出现在边缘,我试图创造的效果是

图像顶部的灰线。中间灰色,两边渐白。

我试过这样的东西

  func render(content: FeedItem) {
        print(content.item.externalId)

//        rowSeperatorView.backgroundColor = UIColor.usingHex("f2f2f2")
        iconContainerView.backgroundColor = UIColor.usingHex("3bac58")


        let gradientLayer: CAGradientLayer = CAGradientLayer()
        gradientLayer.frame = rowSeperatorView.bounds
        gradientLayer.colors = [
            UIColor.white.cgColor,
            UIColor.usingHex("f2f2f2").cgColor,
            UIColor.white.cgColor,
        ]
        gradientLayer.locations = [0,0.5,1]

        rowSeperatorView.layer.addSublayer(gradientLayer)




        iconContainerView.layer.cornerRadius = 5
        iconContainerView.clipsToBounds = true
        iconContainerView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMaxXMinYCorner]
    }

但是好像达不到这个效果

你就快完成了,尝试添加 startPointendPoint,然后使用位置 属性。

    let gradient = CAGradientLayer()
    gradient.frame = rowSeperatorView.bounds
    gradient.colors = [UIColor.white.cgColor, UIColor.usingHex("f2f2f2").cgColor, UIColor.white.cgColor]

    gradient.locations = [0, 0.4, 0.6]
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 1.0, y: 0.0)

    rowSeperatorView.layer.addSublayer(gradient)