我也想使用中心颜色。有人可以帮我解决这个问题吗?
extension UIView {
func setGradientBackground(topColor: UIColor, bottomColor: UIColor) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = [topColor.cgColor, bottomColor.cgColor]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 0.0, y: 1.0)
layer.insertSublayer(gradientLayer, at: 0)
}
}
最佳答案
只需添加:
gradientLayer.colors = [topColor.cgColor, <centerColor.cgColor>,bottomColor.cgColor]
gradientLayer.locations = [0.0, 0.5, 1.0] //you can remove this line if you want even gradient.
gradientLayer.colors
和 gradientLayer.locations
不限于每个仅两个输入。
此外,startPoint
和 endPoint
分别默认为 (0.5,0.0) 和 (0.5,1.0)。您可以在这里引用它的样子:
关于swift - 如何使用3种颜色制作UIView渐变效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59377440/