Arsen Arsen - 1 year ago 397
Swift Question

CAShapeLayer 1px dash line

I'm trying to draw one pixel dash line but it always draws 2px line. Current solution:

let shapelayer = CAShapeLayer()
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: x, y: y))
path.addLineToPoint(CGPoint(x: x, y: y))

shapelayer.strokeColor = UIColor.whiteColor().CGColor
shapelayer.lineWidth = 1.0 / UIScreen.mainScreen().scale
shapelayer.lineJoin = kCALineJoinMiter
shapelayer.lineDashPattern = [1, 3]
shapelayer.path = path.CGPath

Answer Source

That's because the shape layer doesn't know you have a double-resolution screen, so it is drawing a 1 point line and it's being doubled to 2 pixels on the screen. You need to set the shape layer's contentsScale to your screen's display scale.

This issue arises only for layers you create yourself (like this one).