lineDashPattern
The dash pattern applied to the shape’s path when stroked.
Declaration
var lineDashPattern: [NSNumber]? { get set }Discussion
The dash pattern is specified as an array of NSNumber objects that specify the lengths of the painted segments and unpainted segments, respectively, of the dash pattern.
For example, passing an array with the values [2,3] sets a dash pattern that alternates between a 2-user-space-unit-long painted segment and a 3-user-space-unit-long unpainted segment. Passing the values [10,5,5,5] sets the pattern to a 10-unit painted segment, a 5-unit unpainted segment, a 5-unit painted segment, and a 5-unit unpainted segment.
Default is nil, a solid line.
The following code shows how how you can create three shape layers using the dash patterns described above. Each shape layer contains a simple path that describes a horizontal line.
let layer = CALayer()
let lineDashPatterns: [[NSNumber]?] = [nil, [2,3], [10, 5, 5, 5]]
for (index, lineDashPattern) in lineDashPatterns.enumerated() {
let shapeLayer = CAShapeLayer()
shapeLayer.strokeColor = UIColor.black.cgColor
shapeLayer.lineWidth = 5
shapeLayer.lineDashPattern = lineDashPattern
let path = CGMutablePath()
let y = CGFloat(index * 50)
path.addLines(between: [CGPoint(x: 0, y: y),
CGPoint(x: 640, y: y)])
shapeLayer.path = path
layer.addSublayer(shapeLayer)
}The following figure shows three shape layers created with the code above. The top solid line has a nil lineDashPattern, the middle has [2,3] and the bottom has [10,5,5,5].
[Image]