ios PDFKit displaymode = одна страница показывает только первую страницу PDF - PullRequest
2 голосов
/ 12 марта 2019

Я пытаюсь отобразить PDF на ios через библиотеку яблок PDFKit, и вместо того, чтобы использовать режим PDFDisplayMode.singlePageContinuous, я хочу остановиться на разрывах страниц, поэтому я пытаюсь использовать PDFDisplayMode.singlePage.https://developer.apple.com/documentation/pdfkit/pdfdisplaymode

Однако этот режим, кажется, отображает только одну страницу PDF, что совершенно бесполезно.Я попытался добавить обработчики смахивания на страницу, но они тоже не работают.

Я нашел примеры приложений и изменил их код для тестирования pdfdisplaymode, но получил ту же проблему, например https://github.com/vipulshah2010/PDFKitDemo

Как я могу реализовать одну страницу за раз pdfviewer с pdfkit, чтопозволяет перелистывать страницы ?!

Ответы [ 3 ]

2 голосов
/ 29 апреля 2019

Еще один простой способ сделать это - установить

pdfView.usePageViewController(true) 

Это добавляет пролистывание между страницами для вас и не нужно настраивать свои собственные жесты. Смотрите пример ниже:

override func viewDidLoad() {
    super.viewDidLoad()

    // Add PDFView to view controller.
    let pdfView = PDFView(frame: self.view.bounds)
    self.view.addSubview(pdfView)

    // Configure PDFView to be one page at a time swiping horizontally
    pdfView.autoScales = true
    pdfView.displayMode = .singlePage
    pdfView.displayDirection = .horizontal
    pdfView.usePageViewController(true)

    // load PDF
    let webUrl: URL! = URL(string: url)
    pdfView.document = PDFDocument(url: webUrl!)
}
2 голосов
/ 12 марта 2019

Используйте распознаватель жестов смахивания (UISwipeGestureRecognizer), чтобы позволить пользователю смахивать экран просмотра PDF (PDFView) влево и вправо.

import UIKit
import PDFKit

class ViewController: UIViewController, PDFViewDelegate {
    // MARK: - Variables

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

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

        let filePath = "/Users/george/Library/Developer/CoreSimulator/Devices/B5C5791C-3916-4BCB-8EB6-5D3D61C08DC0/data/Containers/Data/Application/4B644584-0025-45A7-9D71-C8F8478E4620/Documents/my PDF.pdf"
        pdfView.document = getDocument(path: filePath)
        pdfView.backgroundColor = .lightGray
        pdfView.autoScales = true
        pdfView.displayMode = .singlePageContinuous
        pdfView.usePageViewController(true, withViewOptions: nil)
        createMenu()
        thumbnail()

        /* swipe gesture */
        let leftSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(respondLeftSwipeGesture(_:)))
        leftSwipeGesture.direction = [UISwipeGestureRecognizer.Direction.left]
        self.view.addGestureRecognizer(leftSwipeGesture)
        let rightSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(respondRightSwipeGesture(_:)))
        rightSwipeGesture.direction = [UISwipeGestureRecognizer.Direction.right]
        pdfView.addGestureRecognizer(rightSwipeGesture)
    }

    @objc func respondLeftSwipeGesture(_ sender: UISwipeGestureRecognizer) {
        if pdfView.document == nil { return }
        pdfView.goToPreviousPage(self)
    }

    @objc func respondRightSwipeGesture(_ sender: UISwipeGestureRecognizer) {
        if pdfView.document == nil { return }
        pdfView.goToNextPage(self)
    }

    func getDocument(path: String) -> PDFDocument? {
        let pdfURL = URL(fileURLWithPath: path)
        let document = PDFDocument(url: pdfURL)
        return document
    }
}
0 голосов
/ 14 мая 2019

Вы можете просто установить displayMode на непрерывный режим, и он может работать:

pdfView.displayMode = .singlePageContinuous
...