PDFKit无法更新注释位置

PDFKit无法更新注释位置

本文介绍了iOS 11 PDFKit无法更新注释位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个可在iPad上编辑PDF的应用程序.

I'm building an app to edit PDFs on iPad.

我正在尝试使用添加到PDFView的superview中的panGesture识别器来实现注释的拖动.问题在于注释的新rect边界已分配,但更改未反映在屏幕上.

I'm trying to implement the dragging of the annotations with a panGesture recognizer that I added to the superview of the PDFView. The problem is that the new rect bounds of the annotation gets assigned but the changes doesn't reflect on screen.

这是我的代码:

@objc func handlePanGesture(panGesture: UIPanGestureRecognizer) {
    let touchLocation = panGesture.location(in: pdfView)

    guard let page = pdfView.page(for: touchLocation, nearest: true) else {
        return
    }
    let locationOnPage = pdfView.convert(touchLocation, to: page)

    switch panGesture.state {
    case .began:

           guard let annotation = page.annotation(at: locationOnPage) else {
                return
            }
            currentlySelectedAnnotation = annotation
    case .changed:

        guard let annotation = currentlySelectedAnnotation else {
            return
        }
        let initialBounds = annotation.bounds
        annotation.bounds = CGRect(origin: locationOnPage,
                                   size: initialBounds.size)

        print("move to \(locationOnPage)")
    case .ended, .cancelled, .failed:
        break
    default:
        break
    }
}

希望你能帮助我.

推荐答案

好吧,因为没有人回答.我认为该框架中存在一个错误,因此,经过一段时间的反复试验,我将发布对我有用的内容.

Well, Since nobody replied. I think there is a bug in the framework, so I'll post what worked for me, after some time of trial and error.

let initialBounds = annotation.bounds
annotation.bounds = CGRect(
        origin: locationOnPage,
        size: initialBounds.size)
page.removeAnnotation(annotation)
page.addAnnotation(annotation)

虽然不优雅,但确实可以做到

It's not elegant, but it does the job

这篇关于iOS 11 PDFKit无法更新注释位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:43