NOTE that I looked a lot everywhere but couldn't find a solution.
Using the Storyboard, I have a ViewController, with a View (UIView) and a Subview (PKCanvasView) where I want to use PencilKit to draw on the latter.
_
My objectives are:
Draw only on this PKCanvasView
Get the coordinates in real time of where I'm drawing on the PKCanvasView
My results:
I can draw on the PKCanvasView only if I start from this view, and I can only draw within the frame of this PKCanvasView. That's good.
I can only manage to get the coordinates of where I'm touching the screen IF I start the gesture on the UIView. BUT Not if I touch the PKCanvasView first.
My problem:
How do I get the coordinates of where I'm drawing on the PKCanvasView?
–
Below is my code:
import UIKit
import PencilKit
class ViewController: UIViewController {
@IBOutlet weak var labelText: UILabel! //Label object to display the coordinates
@IBOutlet weak var canvasView: PKCanvasView! //Subview to draw on
override func viewDidAppear(_ animated: Bool) {
canvasView.tool = PKInkingTool(.pen, color: .black, width: 10)
}
// MARK: FUNCTIONS //
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touches.forEach { (touch) in
updateGagues(with: touch)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
touches.forEach { (touch) in
updateGagues(with: touch)
}
}
private func updateGagues(with touch: UITouch) {
let location = touch.preciseLocation(in: view)
labelText.text = location.debugDescription
}
}