Swift UIView: make a path always be at the end of person's finger?

Viewed 895

OK, I am new to UIBezierPaths but I need to have a path whose endpoint updates according to where the user's finger is. It should change in touchesMoved

So far I have:

func customInit() {
    print("init scene")
    self.backgroundColor = UIColor.cyan

    path.move(to: CGPoint(x: 0, y: 0))
    path.addLine(to: CGPoint(x: 300, y: 300))

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    shapeLayer.strokeColor = UIColor.blue.cgColor
    shapeLayer.lineWidth = 3.0

    self.layer.addSublayer(shapeLayer)
}

override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    var touch : UITouch! =  touches.first! as UITouch
    var location = touch.location(in: self)

    path.move(to: location)
}

I thought this would update the path endpoint but nothing happens beyond drawing the original line. I would like to not use SKScene for this, but don't know what is going wrong.

How can I make a line always have a point at the point of user's tap?

1 Answers
Related