【发布时间】:2016-11-30 18:38:12
【问题描述】:
我在使用 UIBezierPath 时遇到了一个奇怪的问题,我的颜色和线宽没有正确呈现。
我正在绘制一系列具有主要和次要渐变的线条(如尺子)。主要渐变是长度较长的线条......
主要渐变:红色和线宽:2.0 小渐变:黄色和线宽:0.0(根据规格最细的线)
override func draw(in ctx: CGContext) {
if let _ = cameraControlSlider {
guard gradationValues.count >= 0 else {
return
}
let frame = CGRect(x: insetBy, y: 0, width: bounds.width - insetBy, height: bounds.height)
ctx.clear(frame)
let startx = Int(frame.origin.x)
let endx = Int(frame.origin.x + frame.width)
let incrementWidth = (endx - startx) / (gradationValuesOnScreen + 1)
var counter = 1
var x = startx + counter * incrementWidth
while x < endx {
var y1 = Int(frame.origin.y)
var y2 = Int(frame.origin.y) + Int(frame.height)
var lineWidth: Float = 2.0
var color = UIColor.red
if counter % majorGradationInterval != 1 {
y1 = Int(frame.origin.y) + Int(frame.height / 4)
y2 = Int(frame.origin.y + frame.height) - Int(frame.height / 4)
lineWidth = 0.0
color = UIColor.yellow
}
ctx.addPath(drawLine(in: ctx, x1: x, y1: y1, x2: x, y2: y2, color: color, lineWidth: lineWidth))
counter += 1
x = startx + counter * incrementWidth
}
}
}
func drawLine(in ctx: CGContext, x1: Int, y1: Int, x2: Int, y2: Int, color: UIColor, lineWidth: Float) -> CGPath {
let path = UIBezierPath()
ctx.setStrokeColor(color.cgColor)
path.lineWidth = CGFloat(lineWidth)
path.move(to: CGPoint(x: x1, y: y1))
path.addLine(to: CGPoint(x: x2, y: y2))
path.close()
ctx.strokePath()
return path.cgPath
}
基本上我希望较长的线条是红色和较粗的线条宽度,但颜色看起来是倒置的,线条宽度似乎没有任何影响。
从 draw 函数中可以看出,没有 UIColor red 与较小的线条长度相关联。我曾尝试在创建路径之前设置笔触颜色,但这似乎不起作用。
【问题讨论】:
标签: ios swift3 uibezierpath