如何解决如何在绘图时获取 Apple Pencil 的值 - penkit
我想在绘图时获取 Apple Pencil 的值(位置、力、方位角)。我用了铅笔盒。我的问题是我是否启用了 PKCanvasView 的用户交互。我可以画画,但我无法获得价值。但是,如果我禁用用户交互,则无法绘制但可以获得值。我该如何解决?
override func viewDidLoad() {
super.viewDidLoad()
// Set up the canvas view with the first drawing from the data model.
canvasView.delegate = self
canvasView.alwaysBounceVertical = true
// Set up the tool picker
if #available(iOS 14.0,*) {
toolPicker = PKToolPicker()
} else {
// Set up the tool picker,using the window of our parent because our view has not
// been added to a window yet.
let window = parent?.view.window
toolPicker = PKToolPicker.shared(for: window!)
}
toolPicker.setVisible(true,forFirstResponder: canvasView)
toolPicker.addobserver(canvasView)
toolPicker.addobserver(self)
canvasView.becomeFirstResponder()
}
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) {
txtForce = touch.force.valueFormattedFordisplay ?? ""
let azimuthUnitVector = touch.azimuthUnitVector(in: canvasView)
txtAzimuthUnitVector = azimuthUnitVector.valueFormattedFordisplay ?? ""
let azimuthAngle = touch.azimuthAngle(in: canvasView)
txtAzimuthAngle = azimuthAngle.valueFormattedFordisplay ?? ""
// When using a finger,the angle is Pi/2 (1.571),representing a touch perpendicular to the device surface.
txtAltitudeAngle = touch.altitudeAngle.valueFormattedFordisplay ?? ""
let location = touch.preciseLocation(in: canvasView)
txtLocation = location.valueFormattedFordisplay ?? ""
print("Location: \(txtLocation)")
print("Force: \(txtForce)")
print("AzimuthUnitVector: \(txtAzimuthUnitVector)")
print("AzimuthAngle: \(txtAzimuthAngle)")
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。