How to draw a circular hatch pattern for CAShapeLayer?

I am trying to stroke paths in CAShapeLayer using dash patterns. I created many traits using the lineDashPattern property. But this property does not provide any option for the type of hatch dash. I want my dashes to be round dots, as in the image below:

enter image description here

I know that this can be achieved because I have seen this in many applications. But I can’t find a way to implement this in the CAShapeLayer library. So, I want to know how to get this round barcode?

+6
source share
3 answers

All you have to do is set kCALineCapRound for the lineCap property of your CAShapeLayer, and then set lineDashPattern to [0.0, x], where, for example, x is 2 * the line width of your CAShapelayer, if you want the border between the points to be same as the diameter of your dots.

0.0 for the first colored segment in lineDashPattern calls kCALineCapRound draws a semicircle in front of and behind your colored segment, and any colored segment greater than zero causes a dot to become a tablet. enter image description here

+4
source

try // swift

 layer.strokeColor = UIColor.whiteColor().CGColor layer.fillColor = UIColor.clearColor().CGColor layer.lineWidth = 4 layer.lineDashPattern = [0.01, layer.lineWidth * 2] layer.lineCap = kCALineCapRound 
+3
source

Try the below snippet

 shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2 * stroke.width()], nil]; shapeLayer.lineCap = kCALineCapRound; 
+1
source

Source: https://habr.com/ru/post/978540/


All Articles