私はどこでもよく見ましたが、解決策を見つけることができなかったことに注意してください。
Storyboardを使用して、ビュー(UIView) とサブビュー (PKCanvasView) を備えたViewControllerがあり、PencilKit を使用して後者に描画します。
_
私の目標は次のとおりです。
このPKCanvasViewのみに描画します
PKCanvasViewで描画している場所の座標をリアルタイムで取得する
私の結果:
このビューから開始する場合にのみPKCanvasViewに描画でき、このPKCanvasViewのフレーム内にのみ描画できます。それは良い。
UIViewでジェスチャを開始した場合にのみ、画面に触れている場所の座標を取得できます。しかし、最初にPKCanvasViewに触れた場合はそうではありません。
私の問題:
PKCanvasView で描画している場所の座標を取得するにはどうすればよいですか?
–</p>
以下は私のコードです:
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
}
}