如何在CAShapeLayer中绘制垂直虚线?

时间:2018-07-15 07:12:17

标签: ios swift core-graphics

我可以用代码绘制水平线:

let lineLayer = CAShapeLayer()    
lineLayer.lineDashPattern = [4, 4]

我要如何创建“条形图”: enter image description here

我创建了条形图,但是无法添加垂直虚线。 CALayer 只有一种方法。

1 个答案:

答案 0 :(得分:2)

要绘制垂直虚线,可以执行以下操作:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let lineLayer = CAShapeLayer()
        lineLayer.strokeColor = UIColor.gray.cgColor
        lineLayer.lineWidth = 2
        lineLayer.lineDashPattern = [4,4]
        let path = CGMutablePath()
        path.addLines(between: [CGPoint(x: 30, y: 50),
                                CGPoint(x: 30, y: 300)])
        lineLayer.path = path
        self.view.layer.addSublayer(lineLayer)
    }

}

这给出了这个结果:

vertically dashed line