import UIKit
import PDFKit
import composeApp
class IOSNativeViewFactory: NativeViewFactory {
func createPdfView(path: String) -> UIView {
let filePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
.first?.appendingPathComponent(path)
let pdfView = PDFView()
pdfView.autoScales = true // Enables pinch-to-zoom
pdfView.displayMode = .singlePageContinuous // Enables scrolling
pdfView.displayDirection = .vertical
pdfView.minScaleFactor = 1.0
pdfView.maxScaleFactor = 4.0 // Adjust zoom limits
pdfView.scaleFactor = 1.5
if let filePath {
if let document = PDFDocument(url: filePath) {
pdfView.document = document
}
}
return pdfView
}
}
确定pdfview的尺寸适当 确保PDFView适当尺寸并添加到视图层次结构中。如果pdfview的尺寸不正确,则可能无法响应滚动手势corkel
pdfView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
pdfView.topAnchor.constraint(equalTo: view.topAnchor),
pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
if let filePath = filePath {
if let document = PDFDocument(url: filePath) {
pdfView.document = document
} else {
print("Failed to load PDF document")
}
} else {
print("Invalid file path")
}
缩放卷轴行为
pdfView.backgroundColor = .lightGray // Set a background color to see the bounds