iOS 11 PDFKit不更新注释位置

时间:2017-10-13 15:55:17

标签: swift ios11 pdfkit

我正在构建一个应用程序来在iPad上编辑PDF。

我尝试使用我添加到PDFView超级视图的panGesture识别器来实现拖动注释。问题是注释的新矩形边界已被分配,但更改并未反映在屏幕上。

这是我的代码:

@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
    }
}

希望你能帮助我。

3 个答案:

答案 0 :(得分:2)

好吧,因为没有人回复。我认为框架中存在一个错误,所以在经过一段时间的反复试验之后,我会发布对我有用的内容。

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

它并不优雅,但却能胜任

答案 1 :(得分:0)

使用Bezier路径,整个bezier路径在边界变化时移动。

PDF的内置线型不会因为更改边界而移动,因此必须在每次更改时设置startPoint和endPoint。

答案 2 :(得分:0)

我在代码中添加了一行,以便在拖动时将注释中心放在手指拖动的位置

    case .changed:

        guard let annotation = currentlySelectedAnnotation else {
            return
        }
        let initialBounds = annotation.bounds
        // Set the center of the annotation to the spot of our finger
        annotation.bounds = CGRect(x: locationOnPage.x - (initialBounds.width / 2), y: locationOnPage.y - (initialBounds.height / 2), width: initialBounds.width, height: initialBounds.height)


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