我正在使用 ios11 中的新 PDFKit 开发 PDF 查看器。我有一个长达 90 页的 pdf 文档作为我的测试文档。重新加载、查看、滚动和导航文档似乎一切正常。我在导航栏中添加了一个分段控件,其中包含 2 个分段,用户可以点击这些分段将 pdfView displayMode 从 singlePageContinuous 切换到 twoUpContinuous,并且它本身也可以正常工作。加载pdf时会出现问题,然后我先捏放大pdfView点击segmentedControl更改displayMode ...组合捏合缩小,然后点击segmentedControl更改为twoUpContinuous使pdfView失去对文档中页面的跟踪,它最终向我显示最后一页,即页面说87、88、89 和 90 的文件。再次点击 segmentedControl 以返回到 singlePageContinuous 导致我仍然在文档的末尾。
当我滚动时,我尝试在页面更改时设置 PDFView 观察者,将该页面保存到本地 var,然后调用 pdfView.go(to: currentVisiblePage) 等来尝试规避这个问题,但不知何故 pdfView 失去了对正确页面的跟踪,因为 segmentedControl 是轻拍。
我的一些代码
private func createPDFView() {
// Configure pdfView
pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
// Position the pdf view
pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
// Configure the toolView (created in storyboard)
view.bringSubview(toFront: toolView)
pdfThumbnailView.backgroundColor = Constants.Colors.collectionViewBackground
view.bringSubview(toFront: pdfThumbnailView)
private func addPageObserver() {
//Wednesday, 31 January 2018 trying to get the page number from the notifciation but not easy
pageObserver = NotificationCenter.default.addObserver(forName: Notification.Name.PDFViewPageChanged, object: nil, queue: OperationQueue.main) { [weak self] (notification) in
print("In pageObserver - currentVisiblePage is : \(String(describing: self?.pdfView.currentPage))")
self?.currentVisiblePage = self?.pdfView.currentPage
}
}
@IBAction func segmentedControlTapped(_ sender: UISegmentedControl) {
// Handle 2 or 4 button displayMode segmented control
switch sender.numberOfSegments {
case 2:
// 2 button case
switch sender.selectedSegmentIndex {
case 0:
pdfView.displayMode = .singlePageContinuous
case 1:
pdfView.displayMode = .twoUpContinuous
default:
pdfView.displayMode = .singlePageContinuous
}
default:
break
}
// Re-layout the document to re-centre if document zoomed by pinch before changing displayMode
pdfView.layoutDocumentView()
//
// Get current page from local var and set to try and fix pagination error
if let currentVisiblePage = currentVisiblePage {
print("in seg control handler ... goint to page: \(String(describing: currentVisiblePage))")
pdfView.go(to: currentVisiblePage)
}
}
我再说一遍,如果在我更改 displayMode 之前没有进行缩放,那根本没有问题。任何帮助或指针表示赞赏