I would like the user to touch 2 points and then a line is drawn between those two points. Here is what I have so far:
func drawline(){
let context = UIGraphicsGetCurrentContext()
context!.beginPath()
context?.move(to: pointA)
context?.addLine(to: pointB)
context!.strokePath()
}
pointA
is the first point the user touched and pointB
is the second point. I get the error:
thread 1:EXC_BREAKPOINT
Thanks in advance for your help.
Draw line in Swift 4.1
To draw a line between two points the first thing you need is get the
CGPoints
from the currentUIView
, there are several ways of achieve this. I going to use anUITapGestureRecognizer
for the sake of the sample to detect when you make a tap.The another step is once you have the two points saved draw the line between the two points, and for this again you can use the graphics context as you try before or use
CAShapeLayer
.So translating the explained above we get the following code:
The above code is going to draw a line every time two points are selected and you can customize the above function as you like.
I hope this help you.