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

问题描述 投票:0回答:1

我正在尝试添加当前页码,因此当用户滚动到另一个页面时,它将显示例如2中的3(如果pdf有3页)

现在,我使用此代码

它将始终显示1中的1

我认为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)"
    }
swift uiscrollview pdfkit
1个回答
3
投票

使用通知(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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.