I'm attempting to draw a line with a UIBezierPath that alternates between dots and dashes. Here's my code so far:
func drawAlternatingDashesAndDots() {
let path = UIBezierPath()
path.move(to: CGPoint(x: 10,y: 10))
path.addLine(to: CGPoint(x: 290,y: 10))
path.lineWidth = 8
let dots: [CGFloat] = [0.001, path.lineWidth * 2]
path.setLineDash(dots, count: dots.count, phase: 0)
for i in 0...10 {
if i % 2 == 0 {
// Even Number
path.lineCapStyle = CGLineCap.round
} else {
// Odd Number
path.lineCapStyle = CGLineCap.square
}
}
UIGraphicsBeginImageContextWithOptions(CGSize(width:300, height:20), false, 2)
UIColor.white.setFill()
UIGraphicsGetCurrentContext()!.fill(.infinite)
UIColor.black.setStroke()
path.stroke()
let image = UIGraphicsGetImageFromCurrentImageContext()
dashDotDashLineView.image = image
UIGraphicsEndImageContext()
}
However, in my image view, only dots are rendered:
I'm probably misunderstanding something about how UIBezierPath works; any help appreciated!

The
UIBezierPathdocumentation explains the meaning of thepatternargument:In your code,
0.001is the first value in the pattern, so it is a line segment length, and is small enough to be drawn as a dot. The second value in the pattern ispath.lineWidth * 2, so it is a gap length, which is the distance between the dots.If you want to alternate dots and dashes, you have to pass a pattern containing four numbers: a line segment length, a gap, another line segment length, and another gap.