首页 > 解决方案 > 如何在iOS(Swift)上同时使用PDFKit中的铅笔(例如墨水)注释和手指触摸导航?

问题描述

我在 Swift 中使用 PDFKit 编写了一个 PDF 注释应用程序,它使用 touchesBegan/Moved/Ended 在 PDF 上进行注释。非常适合绘制注释。

为了获得铅笔触摸事件,如果我进行注释并设置“isUserInteractionEnabled = true”以导航(平移/缩放)PDF文档,我需要制作一个切换按钮以在PDFView上设置“isUserInteractionEnabled = false”。使用“isUserInteraction = true”,所有触摸事件都被 PDFView“吃掉”(我认为它是 documentView Scrollview),并且永远不会在 ViewController 上调用。

永久切换对用户来说真的很烦人并且不可用。

那么如何在 ViewController 中使用 touchesBegan/Moved/Ended 覆盖,并能够通过手指触摸在 PDF 中导航(平移/缩放)而无需一直切换 isUserInteractionEnabled?

该应用程序应仅使用铅笔在 iPad 上运行。

感谢您抽出宝贵时间,拉斯

使事情更清晰的示例实现:(在 ViewController 类中:UIViewController)

override func viewDidLoad() {
    super.viewDidLoad()

    pdfView = PDFView()

    pdfView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(pdfView)

    pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    pdfView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

    let url = Bundle.main.url(forResource: "TestPDF", withExtension: "pdf")!
    pdfView.document = PDFDocument(url: url)

    pdfView.isUserInteractionEnabled = false //touches events are called ... annotation with pencil mode
    //pdfView.isUserInteractionEnabled = true //touches events are NEVER called ... but pan/zoom works
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("touchesBegan called!")

    if let touch = touches.first {
        if touch.type == .stylus {
            print("touchesBegan pencil annotation ...")
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("touchesMoved called!")

    if let touch = touches.first {
        if touch.type == .stylus {
            print("touchesMoved pencil annotation ...")
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    print("touchesEnded called!")

    if let touch = touches.first {
        if touch.type == .stylus {
            print("touchesEnded pencil annotation ...")
        }
    }
}

标签: iosswiftpdfkitpdfview

解决方案


You can implement all your drawing operations in PDFView's UIGestureRecognizer and implement this method also.

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
{
    if touch.type == .stylus
    {
        return true
    }
    else
    {
        return false
    }
}

推荐阅读