我想得到我以后收到的东西
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文件的宽度。
答案 0 :(得分:1)
使用
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
}
答案 1 :(得分:1)
解决方案是创建一个CustomPDFView
并覆盖它的实例属性 canZoomIn
:
class CustomPDFView: PDFView {
override func canZoomIn() -> Bool {
return false
}
}
然后,@IBOutlet weak var pdfView: PDFView!
的类型应为@IBOutlet weak var pdfView: CustomPDFView!
。还要在IdentyInspector上对其进行更新,以免崩溃。
有关更多信息,请从Apple Docs中查看:canZoomIn
canZoomIn
返回一个布尔值,该值指示用户是否可以放大 查看和放大。
声明
var canZoomIn: Bool { get }
答案 2 :(得分:1)
我尝试了各种方法来禁用捏缩放,包括覆盖canZoomIn(我发现捏缩放未调用),设置委托人的pdfViewWillChangeScaleFactor方法(我发现从未调用过)以及将最大和最小缩放因子设置为视图(这将阻止视图自动缩放)。
最后,我可以通过将自己的pinchGestureRecognizer添加到PDFView中以覆盖视图的内置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
}