我也想使用中间色。有人可以帮我吗?
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)。您可以在这里参考它的外观: