私はのUIViewControllerとUIGestureRecognizerDelegateを拡張するPDFViewControllerクラスを作成することで問題を解決しました。私はサブビューとしてPDFViewを追加し、annotationモードを切り替える役目をするnavigationItemにUIBarButtonItemを追加しました。注釈トグルボタンがちょうど走る
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: pdfView)
signingPath = UIBezierPath()
signingPath.move(to: pdfView.convert(position, to: pdfView.page(for: position, nearest: true)!))
annotationAdded = false
UIGraphicsBeginImageContext(CGSize(width: 800, height: 600))
lastPoint = pdfView.convert(position, to: pdfView.page(for: position, nearest: true)!)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: pdfView)
let convertedPoint = pdfView.convert(position, to: pdfView.page(for: position, nearest: true)!)
let page = pdfView.page(for: position, nearest: true)!
signingPath.addLine(to: convertedPoint)
let rect = signingPath.bounds
if(annotationAdded) {
pdfView.document?.page(at: 0)?.removeAnnotation(currentAnnotation)
currentAnnotation = PDFAnnotation(bounds: rect, forType: .ink, withProperties: nil)
var signingPathCentered = UIBezierPath()
signingPathCentered.cgPath = signingPath.cgPath
signingPathCentered.moveCenter(to: rect.center)
currentAnnotation.add(signingPathCentered)
pdfView.document?.page(at: 0)?.addAnnotation(currentAnnotation)
} else {
lastPoint = pdfView.convert(position, to: pdfView.page(for: position, nearest: true)!)
annotationAdded = true
currentAnnotation = PDFAnnotation(bounds: rect, forType: .ink, withProperties: nil)
currentAnnotation.add(signingPath)
pdfView.document?.page(at: 0)?.addAnnotation(currentAnnotation)
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: pdfView)
signingPath.addLine(to: pdfView.convert(position, to: pdfView.page(for: position, nearest: true)!))
pdfView.document?.page(at: 0)?.removeAnnotation(currentAnnotation)
let rect = signingPath.bounds
let annotation = PDFAnnotation(bounds: rect, forType: .ink, withProperties: nil)
annotation.color = UIColor(hex: 0x284283)
signingPath.moveCenter(to: rect.center)
annotation.add(signingPath)
pdfView.document?.page(at: 0)?.addAnnotation(annotation)
}
}
:
pdfView.isUserInteractionEnabled = !pdfView.isUserInteractionEnabled
を
私はUIBezierPathでタッチがsigningPathと呼ばれる記録し、次のコードを使用して型PDFAnnotationのcurrentAnnotationで現在の注釈を持っていますこれは本当にPDFのスクロールを無効にし、タッチイベントを受け取ることができるので、実際にはその鍵でした。
タッチイベントが記録されてPDFAnnotationに変換される方法は、PDFに書き込むときに注釈が表示され、最後にスクロール位置に関係なくPDFの正しい位置に記録されることを意味します。
正しいページで終わることを確認することは、ページ番号のハードコーディングされた0をpdfView.page(for:position、nearest:true)の値に同様に変更するだけです。
このコードで 'UIGraphicsBeginImageContext(CGSize(width:800、height:600))'が実行されているかどうかは不明です。 –
申し訳ありませんが、私はここに投稿するためのコードを準備していませんでした - 他の人が興味を持っていたので私は自分のコードを入れなければならなかったので、ここに例では必要ないコードが潜在的に含まれています私のアプリのための他の目的のために(まだ未完成です)。私が覚えている限りでは、他の人が文脈が開始されていないという警告を受けたので、コード行がそこにありました。 – jksoegaard
あなたが私を誤解した場合、それは否定的なレビューではありませんでした。私はそれがなぜ必要なのか、それが何のために役立ったのか疑問に思いました。 私は自分の小さなUIGestureRecognizerをPDFView用にしましたが、それがなくても動作することを確認できます(シミュレータ上で警告が表示されませんでした。おそらく、デバイス上で、それは異なります)。 –