swift 如何在pdfView中禁用放大和缩小?

y53ybaqx  于 2023-01-19  发布在  Swift
关注(0)|答案(4)|浏览(202)

我只想要我收到的东西

pdfView.autoScales = true

而不可能改变比例。
在@carmine的帮助下,我们有:

import UIKit
import PDFKit

class ViewController: UIViewController {

@IBOutlet weak var pdfView: PDFView!

override func viewDidLoad() {
    super.viewDidLoad()

    setPdfFile()
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    self.pdfView.autoScales = true
}

func setPdfFile() {
    if let path = Bundle.main.path(forResource: "Swift.org - Getting Started", ofType: "pdf") {
        let url = URL(fileURLWithPath: path)
        if let pdfDocument = PDFDocument(url: url) {
            pdfView.autoScales = true
            pdfView.displayMode = .singlePageContinuous
            pdfView.displayDirection = .vertical
            pdfView.document = pdfDocument

            pdfView.displaysPageBreaks = false

            pdfView.minScaleFactor = pdfView.scaleFactor
            pdfView.maxScaleFactor = pdfView.scaleFactorForSizeToFit
            }
        }
    }
}

这给出了这样的结果:https://gfycat.com/briefpowerlessbug
但如果运行时没有这些行:

pdfView.minScaleFactor = pdfView.scaleFactor
            pdfView.maxScaleFactor = pdfView.scaleFactorForSizeToFit

您将收到我需要的结果,但缩放:https://gfycat.com/presentconcretebunting
我想禁用缩放并使pdfView的宽度等于pdf文件的宽度。

7fhtutme

7fhtutme1#

我尝试了各种禁用收缩缩放的方法,包括覆盖canZoomIn(我发现收缩缩放时没有调用它),设置代理的pdfViewWillChangeScaleFactor方法(我发现它从未被调用),以及设置视图上的最大和最小比例因子(这会阻止视图自动缩放)。
最后,我通过向PDFView添加我自己的pinchGestureRecognizer来覆盖视图的内置pinchGestureRecognizer,从而实现了我想要的。为了使pinchGestureRecognizer具有优先级,您需要为识别器创建一个委托并覆盖以下方法:

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
         shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {

        //Prefer this pinch recognizer over the default one to override pinch zoom
        if gestureRecognizer is UIPinchGestureRecognizer && otherGestureRecognizer is UIPinchGestureRecognizer {
            return true
        }
    }
   return false
}
axzmvihb

axzmvihb2#

用途

pdfView.minScaleFactor = pdfView.scaleFactor
 pdfView.maxScaleFactor = pdfView.scaleFactorForSizeToFit

并加上

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    self.pdfView.autoScales = true
}
lh80um4z

lh80um4z3#

解决方法是创建一个CustomPDFView并覆盖它的示例属性canZoomIn

class CustomPDFView: PDFView {
    override func canZoomIn() -> Bool {
        return false
    }
}

那么@IBOutlet weak var pdfView: PDFView!应该是@IBOutlet weak var pdfView: CustomPDFView!类型。还要在IdentyInspector上更新它,这样它就不会让你崩溃。
欲了解更多信息,请查看:来自Apple文档的canZoomIn

可以放大

返回一个布尔值,该值指示用户是否可以放大视图。

声明

var canZoomIn: Bool { get }
bxgwgixi

bxgwgixi4#

这可以通过将minScaleFactor和maxScaleFactor设置为相同的值来轻松实现,例如scaleFactorForSizeToFit。

pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
pdfView.maxScaleFactor = pdfView.minScaleFactor

相关问题