Reputation: 16149
I want to draw a dashed line and a solid line in one context. But if I draw a dashed line first, the second line is also a dashed line. So how to clean up context.setLineDash
? I tried context.beginContext()
, it doesn't help.
Here is the code:
if let context = UIGraphicsGetCurrentContext() {
// The first line
let startPoint = CGPoint(x: 20, y: rect.height/2)
let endPoint = CGPoint(x: rect.width - 40, y: rect.height/2)
context.move(to: startPoint)
context.addLine(to: endPoint)
context.setLineWidth(1)
context.setLineDash(phase: 0, lengths: [4, 4])
context.setStrokeColor(UIColor.progressDashLineColor.cgColor)
context.strokePath()
// Second line
let startPoint1 = CGPoint(x: 20, y: rect.height/2 + 5)
let endPoint1 = CGPoint(x: rect.width-120, y: rect.height/2)
context.setStrokeColor(UIColor.mainColor.cgColor)
context.setLineWidth(5)
context.move(to: startPoint1)
context.addLine(to: endPoint1)
context.setLineCap(.round)
context.strokePath()
}
Upvotes: 4
Views: 1114
Reputation: 9777
Set the line dash to an empty array before drawing your solid line.
context.setLineDash(phase: 0, lengths: [])
From the CGContext
documentation:
Pass an empty array to clear the dash pattern so that all stroke drawing in the context uses solid lines.
Upvotes: 5