ios - 使用不同的坐标系注释 PDF

标签 ios swift annotations touch pdfkit

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

    }
}

但是,正如您从我下面的图片中看到的那样(黑线是我触摸屏幕的地方,红线是绘制注释的地方)。触摸坐标系在屏幕左上角 (0,0),pdf 从屏幕左下角 (0,0) 开始注释。

iPad image inserted

我的添加注解功能如下:

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

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

最佳答案

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

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

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

关于ios - 使用不同的坐标系注释 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47251023/

相关文章:

ios - 如何使用 NSMutableArray 填充 TableView

ios - 暂停/播放按钮监听器

java - 可用的java注释重载吗?

java - 为什么@Deprecated 在这里不起作用?

ios - 可以检测我的框架是作为主应用程序还是扩展程序的一部分运行?

ios - 必须导入 objective-c gettimeofday?

ios - Xcode 8 将代码转换为 swift 2.3 不在设备目标中运行

ios - 在连接到 IPv6 网络的 Wi-Fi 上运行 iOS 10.0

ios - 使用 KML 文件 Swift 应用程序

java - 如何使用 Hibernate 获取两个字段的唯一键?