【发布时间】:2018-07-11 03:40:14
【问题描述】:
我正在使用 ios11 中的新 PDFKit 开发 PDF 查看器。我有一个长达 90 页的 pdf 文档作为我的测试文档。重新加载、查看、滚动和导航文档似乎一切正常。我在导航栏中添加了一个分段控件,其中包含 2 个分段,用户可以点击这些分段将 pdfView displayMode 从 singlePageContinuous 交换为 twoUpContinuous,并且它本身也可以正常工作。当pdf加载时会出现问题,然后我捏合以放大pdfView before 点击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 之前没有进行缩放,那根本没有问题。任何帮助或指针表示赞赏
【问题讨论】: