我可以用代码绘制水平线:
let lineLayer = CAShapeLayer()
lineLayer.lineDashPattern = [4, 4]
我创建了条形图,但是无法添加垂直虚线。 CALayer 只有一种方法。
答案 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)
}
}
这给出了这个结果: