11

我正在尝试将使用 webview 显示某些 pdf 的项目移动到 pdfView 以利用最新的 PDFKit 功能。

在 webview 中捏以缩小文档时总是缩放以填满屏幕。基本上你不能缩小它反弹回来填满屏幕的页面。

现在有了 pdfView,我可以通过捏合来缩小,它看起来一点也不好看,没有必要让 pdf 页面小于屏幕...

将手指从屏幕上松开后,有什么方法可以激活自动缩放。我知道有手势功能,但我不熟悉它的用途。

4

4 回答 4

21

Just to confirm that the accepted answer is the correct one, but also to highlight where the code needs to be used (as stated in the comment by answer author). i.e. the code must be used AFTER setting the pdf document:

pdfView.document = pdfDocument
pdfView.autoScales = true
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit 
于 2018-05-13T20:02:21.597 回答
8

回答我自己的问题,其实很简单...

pdfView.autoScales = true 
pdfView.maxScaleFactor = 4.0 
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
于 2017-11-23T22:08:22.533 回答
1

PDFView一旦您设置了文档属性,此解决方案将自动调整。只需使用NoZoomOutPDFView而不是PDFView您的视图来显示PDFDocument.

import Foundation
import PDFKit

final class NoZoomOutPDFView: PDFView {

    init() {
        super.init(frame: .zero)
        NotificationCenter
            .default
            .addObserver(
                self,
                selector: #selector(update),
                name: .PDFViewDocumentChanged,
                object: nil
            )
    }

    deinit {
        // If your app targets iOS 9.0 and later the following line can be omitted
        NotificationCenter.default.removeObserver(self)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    @objc private func update() {
        // PDF can be zoomed in but not zoomed out
        DispatchQueue.main.async {
            self.autoScales = true
            self.maxScaleFactor = 4.0
            self.minScaleFactor = self.scaleFactorForSizeToFit
        }
    }
}
于 2018-04-03T16:26:22.410 回答
0

如果重写 PDFView 的方法受到限制,可以通过添加观察者来完成;

NotificationCenter.default.addObserver(self, selector: #selector(scaleChanged), name: NSNotification.Name.PDFViewScaleChanged, object: nil)

并像这样使用它:

@objc func scaleChanged() {
    self.pdfView.maxScaleFactor = 3.0
    self.pdfView.minScaleFactor = self.pdfView.scaleFactorForSizeToFit
}
于 2020-01-06T12:39:07.000 回答