简体   繁体   中英

How draw dashed vertical lines in CAShapeLayer?

I can draw horizontal line with code:

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

I want create "Bar chart" how this: 在此处输入图片说明

I created Bar chart, but can't add vertical dashed lines. CALayer have one method only.

To draw a vertically dashed line you could do the following:

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)
    }

}

This gives this result:

垂直虚线

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM