我正在尝试使用PDFKit为PDFView添加手势识别器。我使用以下代码:
class ViewController: UIViewController, PDFDocumentDelegate {
@IBOutlet weak var pdfView: PDFView?
/// - Tag: SetDelegate
override func viewDidLoad() {
super.viewDidLoad()
if let documentURL = Bundle.main.url(forResource: "test", withExtension: "pdf") {
if let document = PDFDocument(url: documentURL) {
// Center document on gray background
pdfView?.autoScales = true
pdfView?.backgroundColor = UIColor.lightGray
// 1. Set delegate
document.delegate = self
pdfView?.document = document
}
}
func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: pdfView)
print(currentPoint)
}
}
func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: pdfView)
print(currentPoint)
}
}
func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let currentPoint = touch.location(in: pdfView)
print(currentPoint)
}
}
}
// 2. Return your custom PDFPage class
/// - Tag: ClassForPage
func classForPage() -> AnyClass {
return WatermarkPage.self
}
}
PDFView上附有一个文档(PDFDocument()反过来打开PDFPage。当代码编译时,无法识别触摸。识别PDF页面触摸的最佳方法是什么?
答案 0 :(得分:0)
您可以使用以下内容:
let panAnnotationGesture = UIPanGestureRecognizer(target: self, action: #selector(didPanAnnotation(sender:)))
pdfView.addGestureRecognizer(panAnnotationGesture)
和
@objc func didPanAnnotation(sender: UIPanGestureRecognizer) {
let touchLocation = sender.location(in: pdfView)
...
}