如何使用 PDFkit 检测滚动到 PDF 中的另一页
How to detect scrolling to another page in PDF using PDFkit
我正在尝试添加当前页码
所以当用户滚动到另一个页面时,它会显示
例如
2 of 3(如果 pdf 有 3 页)
目前,我使用此代码
它将始终显示 3 个中的 1 个
我认为 PDFkit 使用 UIScrollView
所以我需要到达滚动视图,这样我就可以检测到何时滚动到另一个页面,然后增加当前页码
我没有找到任何方法来访问 PDFkit 中的滚动视图
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
}
}
我正在尝试添加当前页码 所以当用户滚动到另一个页面时,它会显示 例如 2 of 3(如果 pdf 有 3 页)
目前,我使用此代码
它将始终显示 3 个中的 1 个
我认为 PDFkit 使用 UIScrollView 所以我需要到达滚动视图,这样我就可以检测到何时滚动到另一个页面,然后增加当前页码
我没有找到任何方法来访问 PDFkit 中的滚动视图
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
}
}