Swift - 绘制虚线

    /// 绘制虚线
    /// - Parameters:
    ///   - lineLength: 虚线长度
    ///   - lineSpacing: 虚线间的间距
    ///   - lineColor: 虚线颜色
    ///   - lineWidth: 描边路径时使用的线宽。默认为1。
    func drawDashLine(lineLength: Int, lineSpacing: Int, lineColor : UIColor, lineWidth: CGFloat = 1){
        let shapeLayer = CAShapeLayer()
        shapeLayer.bounds = self.bounds
        shapeLayer.anchorPoint = CGPoint(x: 0, y: 0)
        shapeLayer.strokeColor = lineColor.cgColor
        shapeLayer.lineWidth = lineWidth
        shapeLayer.lineJoin = CAShapeLayerLineJoin.round
        shapeLayer.lineDashPattern = [NSNumber(value: lineLength),NSNumber(value: lineSpacing)]
        let path = CGMutablePath()
        path.move(to: CGPoint(x: 0, y: 0))
        path.addLine(to: CGPoint(x: self.bounds.size.width, y: 0))
        shapeLayer.path = path
        self.layer.addSublayer(shapeLayer)
    }

你可能感兴趣的:(Swift - 绘制虚线)