如何以此向CALayer()添加模糊的颜色?

时间:2018-09-19 07:18:19

标签: ios swift xcode

我正在尝试实现以下目标: enter image description here

但是我得到这个: enter image description here

我正在使用的代码:

let border = CALayer()
border.backgroundColor = UIColor.viewShadowGray().cgColor
border.frame = CGRect(x: 0, y: 0, width: bottomView.frame.size.width, height: 2)
bottomView.layer.addSublayer(border)

    class func viewShadowGray() -> UIColor
    {
        return UIColor(red: 177.0/255.0, green: 177.0/255.0, blue: 179.0/255.0, alpha: 0.7)

    }

1 个答案:

答案 0 :(得分:3)

使用UIBezierPath创建阴影,如下所示。 您可能需要更改不透明度(layer?.shadowOpacity = 0.80)和阴影半径(layer?.shadowRadius = 3.0)才能满足要求。

func addShadow(to view: UIView?) {
    //Adds a shadow to view
    let layer: CALayer? = view?.layer
    layer?.shadowOffset = CGSize(width: 0, height: 3)
    layer?.shadowColor = UIColor.black.cgColor
    layer?.shadowRadius = 3.0
    layer?.shadowOpacity = 0.80
    layer?.shadowPath = (UIBezierPath(rect: CGRect(x: layer?.bounds.origin.x ?? 0.0, y: layer?.bounds.origin.y ?? 0.0, width: layer?.bounds.size.width ?? 0.0, height: (layer?.bounds.size.height ?? 0.0) + 1))).cgPath
}