swift - 如何使用 PDFkit 检测滚动到 PDF 中的另一页

标签 swift uiscrollview pdfkit

我正在尝试添加当前页码 所以当用户滚动到另一个页面时,它会显示 例如 2 of 3(如果 pdf 有 3 页)

现在,我使用这段代码

它将始终显示 1 of 3

我认为 PDFkit 使用 UIScrollView 所以我需要到达 ScrollView,这样我可以在滚动到另一个页面时检测到,然后增加当前页码

我没有找到任何方法来访问 PDFkit 中的 ScrollView

 func showCurrentPageIndex(){

        guard let totalPages = pdfContainerView.document?.pageCount else {return}
        guard let currentPage = pdfContainerView.currentPage else {return}
        guard let currentPageRef = currentPage.pageRef else {return}

let pageIndex = currentPageRef.pageNumber

      totalPageNumbers.text = "\(totalPages)"
      currentPageIndex.text =  "\(pageIndex)"
    }

最佳答案

使用通知(PDFViewPageChanged)。

import UIKit
import PDFKit

class ViewController: UIViewController, PDFViewDelegate {
    // MARK: - Variables
    var totalCount = Int()

    // MARK: - IBOutlet
    @IBOutlet weak var pdfView: PDFView!

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        let filePath = "Some file document path"
        pdfView.document = getDocument(path: filePath)
        if let total = pdfView.document?.pageCount {
            totalCount = total
        }
        pdfView.backgroundColor = .lightGray
        pdfView.autoScales = true
        pdfView.displayMode = .singlePageContinuous
        pdfView.usePageViewController(true, withViewOptions: nil)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        /* notification */
        NotificationCenter.default.addObserver (self, selector: #selector(handlePageChange), name: Notification.Name.PDFViewPageChanged, object: nil)
    }

    func getDocument(path: String) -> PDFDocument? {
        let pdfURL = URL(fileURLWithPath: filePath)
        let document = PDFDocument(url: pdfURL)
        return document
    }
}

关于swift - 如何使用 PDFkit 检测滚动到 PDF 中的另一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55206785/

相关文章:

ios - UIScrollView 缩放不能正常工作

ios - 不同大小的 ScrollView 页面

ios - 我们可以在 Swift 中使用 PDFKit 与其他应用程序中的绘图共享 PDF 吗?

python - 如何在python中将网页转换为pdf,就像打印中的另存为pdf选项一样

python - 在 Google App Engine 中使用 pdfkit 生成 PDF 会引发 Popen 的 AttributeError

ios - 带有 iOS 10 的 iPhone 上的 UIPopoverPresentationController

iphone - 使滚动条在 UIScrollView 上始终可见?

swift - 为什么有些表达式记录结果而其他表达式只显示迭代计数?

ios - 使用 xcode 6 beta6 和部署目标 7.0 的链接器命令失败,退出代码为 1(使用 -v 查看调用)

ios - Swift 同时检测屏幕左右两侧的触摸