我需要根据值定义图形的颜色(例如:绿色表示0-50之间的值,红色表示50-100之间的值)。我尝试使用渐变颜色,但不是预期的颜色。请问有人能帮助我吗?我使用的是Swift 3。
I need the bottom of the graph to be green and upper part as red
答案 0 :(得分: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]
}
}
在viewDidLoad方法中进行declate。
let colors = Colors()
view.backgroundColor = UIColor.clear
let backgroundLayer = colors.gl
backgroundLayer?.frame = Yourview.frame
Yourview.layer.insertSublayer(backgroundLayer!, at: 0)