如何知道用户是否完成了在 PDFView 中选择文本

How to know if user finished selecting text inside PDFView

有一个问题,macCatalyst 不支持鼠标事件,因为 AppKit 没有移植 (maccatalyst) macOS 应用程序。 我需要知道用户何时使用鼠标或触控板使用 PDFKit 选择完 PDF 文件中的某些文本部分。 也许有人有解决方案或者可以建议如何实现 mouseUptouchedEnded 方法之类的想法?

实现您自己的 GestureRecocongiser:

import UIKit.UIGestureRecognizerSubclass

class TouchUpGestureRecogniser: UIGestureRecognizer {
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
        state = UIGestureRecognizerState(rawValue: 3)!
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
        state = UIGestureRecognizerState(rawValue: 4)!
    }
    
}

然后在您的 VC 中,PDFView 所在的位置:

let customGesture = TouchUpGestureRecogniser(target: self, action: #selector(touchUp(_:)))
customGesture.delegate = self
pdfView.addGestureRecognizer(customGesture)

最后:

extension ViewController: UIGestureRecognizerDelegate {
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

注意:如果您在 PDFView 上有超过 1 个手势,请测试它们,如果有任何问题,请使用 UIGestureRecognizerDelegate 函数合并它们。