基于条形图的值的渐变颜色



我需要根据值来定义图形的颜色(例如:green for 0-50和red之间的值之间的值之间的值,对于50-100之间的值(。我尝试使用梯度颜色,但它是不预期。有人可以帮我吗?我正在使用Swift 3。

我需要图形的底部为绿色和上部为红色

此代码的课程

class Colors {
    var gl:CAGradientLayer!
    init() {
        let colorTop =  UIColor(red: 135.0/255.0, green: 155.0/255.0, blue: 36.0/255.0, alpha: 1.0).cgColor
        let colorcenter = UIColor(red: 186.0/255.0, green: 213.0/255.0, blue: 49.0/255.0, alpha: 1.0).cgColor
        let colorBottom = UIColor(red: 224.0/255.0, green: 241.0/255.0, blue: 142.0/255.0, alpha: 1.0).cgColor
        self.gl = CAGradientLayer()
        self.gl.colors = [colorTop, colorcenter, colorBottom]
        self.gl.locations = [0.5,0.8, 1.0]
    }
}

在ViewDidload方法中拒绝。

let colors = Colors()
view.backgroundColor = UIColor.clear
let backgroundLayer = colors.gl
backgroundLayer?.frame = Yourview.frame
Yourview.layer.insertSublayer(backgroundLayer!, at: 0)

最新更新