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!
