I am working on a project where I want that if user touch move in horizontal direction then horizontal line should draw and of user touch move in vertical direction then vertical line should draw. Kindly suggest some solution using Swift. I tried below. But this is drawing free line.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let touch: AnyObject? = touches.first
let lastPoint = touch!.previousLocation(in: holderView)
path.move(to: CGPoint(x: lastPoint.x, y: lastPoint.y))
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
let touch: AnyObject? = touches.first
let currentPoint = touch!.location(in: holderView)
path.addLine(to: CGPoint(x: currentPoint.x, y: currentPoint.y))
//Design path in layer
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.orange.cgColor
shapeLayer.lineWidth = 20.0
holderView.layer.addSublayer(shapeLayer)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
path=UIBezierPath()
}