swift - 如何在 PDFView 中移动 PDFPage 上的 PDFAnnotation?
问题描述
我正在使用 PDFKit 并且类似于 Apple Preview App,在添加 PDF 注释时,我希望用户能够在页面上移动它们。我在 Github 上创建了一个示例项目。
我尝试子类化 PDFView 并覆盖func mouseDown(with event: NSEvent)
and func mouseDragged(with event: NSEvent)
。我可以分享该代码,但我有一种直觉,我正朝着错误的方向前进。(我想我无法让该代码工作的事实证实了这种感觉......)
编辑:我已将以下代码添加到我的 PDFView。它可以工作,但不能优雅地工作。它不像预览应用程序的工作方式那样流畅。只是想在这里学习,所以也许有人有更好的方法?
private var annotationBeingDragged:PDFAnnotation?
override func mouseDown(with event: NSEvent) {
let areaOfInterest = self.areaOfInterest(forMouse: event)
if areaOfInterest.contains(.annotationArea),
let currentPage = self.currentPage,
let annotation = currentPage.annotation(at: self.convert(self.convert(event.locationInWindow, from: nil), to: currentPage)) {
if annotationBeingDragged == nil {
annotationBeingDragged = annotation
} else {
super.mouseDown(with: event)
}
}
else {
super.mouseDown(with: event)
}
}
override func mouseDragged(with event: NSEvent) {
if let annotation = annotationBeingDragged,
let currentPage = self.currentPage {
currentPage.removeAnnotation(annotation)
let currentPoint = self.convert(self.convert(event.locationInWindow, from: nil), to: currentPage)
annotation.bounds = NSRect(origin: currentPoint, size: annotation.bounds.size)
currentPage.addAnnotation(annotation)
}
else {
super.mouseDragged(with: event)
}
}
override func mouseUp(with event: NSEvent) {
if annotationBeingDragged != nil {
annotationBeingDragged = nil
super.mouseDown(with: event)
}
else {
super.mouseUp(with: event)
}
}
解决方案
mouseUp
它在您的方法更改中对您的代码进行了微小的更改。
super.mouseDown(with: event)
至
super.mouseUp(with: event)
它的工作顺利。
推荐阅读
- vuejs3 - 使用vite构建lib时如何设置多个输出
- performance - 有没有办法扩展 RabbitMQ RPC?
- java - 模拟似乎在进入 lambda 方法时失去了存根
- python - Python中类之间的循环继承
- python - Beam 支持融合模式注册表和 avro 消息
- mysql - 使用mysql将纪元时间转换为gmt
- paypal - 离线支付网关提供商
- tensorflow - Tensorflow Keras 输出层形状奇怪的错误
- python - Remove rows based on a Pandas groupby() result
- c# - 通过微服务创建 Unity Streamingassets