As you can see in the Image attached, there are red and green Views on the screen, I want to add a Line between them. How can I do that?
Asked
Active
Viewed 173 times
1

Akash Neeli
- 337
- 4
- 12
-
2add a uiview with width 1 – udi Jul 19 '22 at 09:24
-
1Ya, That could work...but below answer is a more elegant way of Handling this. – Akash Neeli Jul 19 '22 at 09:28
1 Answers
3
func addLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
let line = CAShapeLayer()
let linePath = UIBezierPath()
linePath.move(to: start)
linePath.addLine(to: end)
line.path = linePath.cgPath
line.strokeColor = UIColor.red.cgColor
line.lineWidth = 1
line.lineJoin = CAShapeLayerLineJoin.round
self.view.layer.addSublayer(line)
}
Usage: - addLine(fromPoint: yourFirstView.center, toPoint: yourSecondView.center)
Make sure your views are accessible while calling this function

PRakash
- 69
- 5