我正在使用 CGGradiant 使用从红色到黄色到绿色的三种颜色绘制一个 3/4 的圆形。但是 CGGradiant 总是从淡黄色而不是红色开始颜色。我使用它是因为我在 drawLinearGradient 中给出的起点/终点。但不知道该怎么做。以下是代码和附加结果。任何建议都会有所帮助。
let path1 = UIBezierPath(arcCenter: center,
radius: radius-20,
startAngle: startAngle,
endAngle: endAngle,
clockwise: false)
let strokedPath = path1.cgPath.copy(strokingWithWidth: strokeWidth, lineCap: .butt, lineJoin: .miter, miterLimit: 0)
ctx!.addPath(strokedPath)
ctx?.clip()
let red = UIColor(red: 234.0/255.0, green: 34.0/255.0, blue: 34.0/255.0, alpha:1)
let green = UIColor(red: 254.0/255.0, green: 187.0/255.0, blue: 3.0/255.0, alpha:1)
let blue = UIColor(red: 69.0/255.0, green: 207.0/255.0, blue: 51.0/255.0, alpha:1)
let gradient = CGGradient(
colorsSpace: CGColorSpaceCreateDeviceRGB(),
colors: [red.cgColor, green.cgColor,blue.cgColor] as CFArray,
locations: [0,0.5,1]
)
context!.drawLinearGradient(
gradient!,
start: CGPoint(x: 20, y: 20),
end: CGPoint(x: rect.width-10, y: rect.height-10),
options: []
)