如何使用3种颜色制作UIView渐变效果

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

我也想使用中间色。有人可以帮我吗?

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)
    }
}
swift uiview gradient swift4.2
1个回答
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.colorsgradientLayer.locations不仅限于两个输入。

同样,startPointendPoint分别默认为(0.5,0.0)和(0.5,1.0)。您可以在这里参考它的外观:

http://ios-tutorial.com/create-uiview-gradient-background/

© www.soinside.com 2019 - 2024. All rights reserved.