【问题标题】:Annotating PDFs with different coordinate systems使用不同坐标系注释 PDF
【发布时间】:2018-04-25 08:23:45
【问题描述】:

我正在使用 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

    }
}

但是,如下图所示(黑线是我触摸屏幕的位置,红线是绘制注释的位置)。 touches坐标系在屏幕左上角(0,0),pdf从屏幕左下角(0,0)开始标注。

我的添加注释功能如下:

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

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

【问题讨论】:

    标签: ios swift annotations touch pdfkit


    【解决方案1】:

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

    func convert(_ point: CGPoint, to page: PDFPage) -> CGPoint
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-07
      • 2019-09-02
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多