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

问题描述 投票:0回答:1

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

gradient example

图像顶部的灰线。中间的灰色和两个边缘都褪色为白色。

我尝试过这样的事情

  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]
    }

但似乎无法实现这一结果。

ios swift uiview cagradientlayer
1个回答
1
投票

你几乎在那里,尝试添加一个startPointendPoint然后玩location位置属性。

    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)
© www.soinside.com 2019 - 2024. All rights reserved.