【发布时间】:2020-03-04 16:09:33
【问题描述】:
请注意,我到处寻找,但找不到解决方案。
使用 Storyboard,我有一个 ViewController,带有一个 View (UIView) 和一个 Subview ( PKCanvasView),我想使用 PencilKit 在后者上绘图。
_
我的目标是:
绘图只在这个PKCanvasView
实时获取我在 PKCanvasView
上绘制位置的坐标
我的结果:
只有从这个视图开始,我才能在 PKCanvasView 上绘制,而且我只能在这个 PKCanvasView 的框架内绘制时间>。这很好。
我只能管理获取我触摸屏幕位置的坐标如果我在 UIView上开始手势>。 但不是,如果我先触摸 PKCanvasView。
我的问题:
如何获取我在 PKCanvasView 上绘制位置的坐标?
-
下面是我的代码:
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
}
}
【问题讨论】: