如何为CALayer()添加渐变色

时间:2018-09-18 06:37:01

标签: ios swift xcode

let bottomBorder = CALayer()
bottomBorder.backgroundColor = UIColor.viewShadowGray().cgColor
bottomBorder.frame = CGRect(x: 0, y: view.frame.size.height - 1, width: view.frame.size.width, height: 1)
view.layer.addSublayer(bottomBorder)

如何修改它以向其添加渐变,使其看起来像这样: enter image description here

1 个答案:

答案 0 :(得分:2)

使用CAGradientLayer

let gradientLayer = CAGradientLayer()
gradientLayer.frame = CGRect(x: 0, y: view.frame.size.height - 1, width: view.frame.size.width, height: 1)
gradientLayer.colors = [
    UIColor.white.withAlphaComponent(0.5).cgColor,
    UIColor.white.withAlphaComponent(0.0).cgColor
]
view.layer.addSublayer(gradientLayer)

根据需要调整渐变颜色。