首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PDFView中移动PDFPage上的PDFView?

如何在PDFView中移动PDFPage上的PDFView?
EN

Stack Overflow用户
提问于 2020-10-18 22:27:39
回答 1查看 277关注 0票数 1

我正在使用PDFKit和类似的苹果预览应用程序,当添加PDF注释时,我希望用户能够在页面上移动它们。我已经创建了一个example project on Github

我尝试过对PDFView进行子类化并覆盖func mouseDown(with event: NSEvent)func mouseDragged(with event: NSEvent)。我可以分享这些代码,但我有种直觉,我觉得我走错了方向。(我想这种感觉是因为我不能让代码工作……)

编辑:我已经将以下代码添加到我的PDFView中。它可以工作,但它不是很优雅。它远没有预览应用的工作方式那么流畅。只是想在这里学习,所以也许有人有更好的方法?

代码语言:javascript
复制
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)
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-10-29 17:43:23

它会在mouseUp方法更改中对代码进行细微更改。

代码语言:javascript
复制
super.mouseDown(with: event)

代码语言:javascript
复制
super.mouseUp(with: event)

它的工作很顺利。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64414446

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档