如何知道用户是否完成了 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 函数合并它们。