drawing dashed line using CALayer

后端 未结 8 1427
执笔经年
执笔经年 2020-12-04 16:49

I was able to draw a dashed box, using the following code:

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGRect shapeRect = CGRectMake(0.0f, 0.0f, 200.0f,         


        
8条回答
  •  天涯浪人
    2020-12-04 17:46

    Swift, more compact:

    func addDashedLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
        let line = CAShapeLayer()
        let linePath = UIBezierPath()
        linePath.moveToPoint(start)
        linePath.addLineToPoint(end)
        line.path = linePath.CGPath
        line.strokeColor = UIColor.redColor().CGColor
        line.lineWidth = 1
        line.lineJoin = kCALineJoinRound
        line.lineDashPattern = [4, 4]
        self.layer.addSublayer(line)
    }
    

提交回复
热议问题