title: How to Get the Coordinates of a Touch Gesture on a PKCanvasView using PencilKit? Swift 5 & Xcode 11
body: <p><em>NOTE that I looked a lot everywhere but couldn't find a solution.</em></p>
  
  <p>Using the <strong>Storyboard</strong>, I have a <strong>ViewController</strong>, with a <strong>View</strong> (UIView) and a <strong>Subview</strong> (PKCanvasView) where I want to use <strong>PencilKit</strong> to draw on the latter.</p>
  
  <p>_</p>
  
  <p><strong>My objectives</strong> are:</p>
  
  <ol>
  <li><p><strong>Draw</strong> only on this <em>PKCanvasView</em></p></li>
  <li><p>Get the <strong>coordinates</strong> in real time of where I'm drawing on the <em>PKCanvasView</em></p></li>
  </ol>
  
  <p><strong>My results</strong>:</p>
  
  <ol>
  <li><p><strong>I can draw</strong> on the <em>PKCanvasView</em> only if I start from this view, and I can only draw within the frame of this <em>PKCanvasView</em>. That's good.</p></li>
  <li><p><strong>I can only manage</strong> to get the coordinates of where I'm touching the screen <strong>IF</strong> I start the gesture on the <em>UIView</em>. <strong>BUT Not</strong> if I touch the <em>PKCanvasView</em> first.</p></li>
  </ol>
  
  <p><strong>My problem</strong>:</p>
  
  <p>How do I get the coordinates of where I'm drawing on the PKCanvasView?</p>
  
  <p>–</p>
  
  <p><strong>Below is my code</strong>:</p>
  
  <pre><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&lt;UITouch&gt;, with event: UIEvent?) {
          touches.forEach { (touch) in
              updateGagues(with: touch)
          }
      }
  
      override func touchesMoved(_ touches: Set&lt;UITouch&gt;, 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
      }
  }
  </code></pre>