0

我需要根据值定义图形的颜色(例如:绿色表示 0-50 之间的值,红色表示 50-100 之间的值)。我尝试使用渐变颜色,但它不符合预期。有人可以帮我解决这个问题吗?我正在使用 Swift 3。

我需要图表的底部为绿色,上部为红色

4

1 回答 1

1

为这段代码声明课外

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]
    }
}

declate 在 vi​​ewDidLoad 方法中。

let colors = Colors()
view.backgroundColor = UIColor.clear
let backgroundLayer = colors.gl
backgroundLayer?.frame = Yourview.frame
Yourview.layer.insertSublayer(backgroundLayer!, at: 0)
于 2018-02-23T12:39:31.547 回答