我正在使用PDFKit和类似的苹果预览应用程序,当添加PDF注释时,我希望用户能够在页面上移动它们。我已经创建了一个example project on Github。
我尝试过对PDFView进行子类化并覆盖func mouseDown(with event: NSEvent)和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)
}
}发布于 2020-10-29 17:43:23
它会在mouseUp方法更改中对代码进行细微更改。
super.mouseDown(with: event)至
super.mouseUp(with: event)它的工作很顺利。
https://stackoverflow.com/questions/64414446
复制相似问题