美文网首页
Swift - 绘制虚线

Swift - 绘制虚线

作者: 懒床小番茄 | 来源:发表于2022-09-08 15:56 被阅读0次
    /// 绘制虚线
    /// - 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 - 绘制虚线

      本文链接:https://www.haomeiwen.com/subject/nlsonrtx.html