使用不同坐标系注释 PDF

Tom*_*Tom 0 annotations touch pdfkit ios swift

我正在使用 PDFKIT 在 pdf 上画一条线。我覆盖触摸开始,移动等如下记录我在pdfView的documentView上的触摸如下:

 override func touchesMoved(_ touches: Set<UITouch>,
                           with event: UIEvent?) {
    // 6
    swiped = true
    if let touch = touches.first {
        let currentPoint = touch.location(in: pdfView.documentView)
       drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
        addAnnotation(fromPoint: lastPoint, toPoint: currentPoint)

        // 7
        lastPoint = currentPoint

    }
}
Run Code Online (Sandbox Code Playgroud)

但是,正如您从下面的图片中看到的(黑线是我触摸屏幕的位置,红线是绘制注释的位置)。触摸坐标系是屏幕左上角的 (0,0),而 pdf 是从屏幕左下角 (0,0) 注释的。

插入的 iPad 图像

我的添加注解功能如下:

func addAnnotation(fromPoint: CGPoint, toPoint: CGPoint) {

    let page = pdfView.document?.page(at: 0)
    let bounds = CGRect(x: 0, y: 0, width: 600, height: 600)
    let line = PDFAnnotation(bounds: bounds, forType: .line, withProperties: nil)
    line.startPoint = fromPoint
    line.endPoint = toPoint
    line.startLineStyle = .none
    line.endLineStyle = .none
    line.color = .red

    let border = PDFBorder()
    border.lineWidth = 4.0
    line.border = border
    page?.addAnnotation(line)
}
Run Code Online (Sandbox Code Playgroud)

如何对齐坐标系,以便我在屏幕上触摸的位置是绘制注释的位置?

Tom*_*Tom 6

对于那些感兴趣的人,我通过使用框架附带的以下函数解决了这个问题,该框架将一个点从视图空间转换为页面空间。

func convert(_ point: CGPoint, to page: PDFPage) -> CGPoint
Run Code Online (Sandbox Code Playgroud)

希望这会对你们中的一些人有所帮助。