PDFView - 适合 2 页的高度和宽度启用滚动到下一页

发布于 2025-01-09 14:13:55 字数 970 浏览 0 评论 0原文

我有一个视图,其中包含来自 PDFKitPDFView。我想在屏幕上同时看到 2 个完整页面,并且它们需要适合高度和宽度。我可以看到两种方法,但都不是完美的:

选项 1)使用 .twoUpContinously 显示模式 - 这里它适合宽度,但高度在 PDF 的约 2/3 中被裁剪。

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUpContinuous // !
    view.displayDirection = .vertical
    view.minScaleFactor = view.scaleFactorForSizeToFit
    view.maxScaleFactor = 3
    view.autoScales = true
    return view
}()

选项 2)使用 .twoUp 显示模式 - 这里它适合宽度和高度,但手势识别器不会记录向下滑动,因此用户无法将页面更改为 3 和 4。

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUp // !
    view.displayDirection = .vertical
    view.minScaleFactor = view.scaleFactorForSizeToFit
    view.maxScaleFactor = 3
    view.autoScales = true
    return view
}()

是否有内置的- 这种情况的解决方案..?或者我是否必须添加手动滑动手势识别器并基于 .twoUp 解决方案更改页面..?

I have a view, which contains PDFView from PDFKit. I want to see 2 full pages at once on my screen and they need to fit both height and width. I can see 2 ways of doing it, but neither is perfect:

Option 1) Display mode with .twoUpContinuous - here it fits width, but height is cropped in ~2/3 of the PDF.

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUpContinuous // !
    view.displayDirection = .vertical
    view.minScaleFactor = view.scaleFactorForSizeToFit
    view.maxScaleFactor = 3
    view.autoScales = true
    return view
}()

Option 2) Display mode with .twoUp - here it fits width and height, but gesture recognizer doesn't record swipe down, and as a result user cannot change page to 3 and 4.

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUp // !
    view.displayDirection = .vertical
    view.minScaleFactor = view.scaleFactorForSizeToFit
    view.maxScaleFactor = 3
    view.autoScales = true
    return view
}()

Is there any built-in solution for such case..? Or do I have to add manually swipe gesture recognizer and change pages based on that with .twoUp solution..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2025-01-16 14:13:55

所以最后我想到了3个解决方案,但都不是完美的。我最喜欢解决方案3。


解决方案 1:不稳定,有时无法渲染 pdf 或启动时不在第一页。但大多数情况下工作正常。

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUpContinuous
    view.autoScales = false
    view.displayDirection = .vertical
    return view
}()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    updatePDFViewScaling()
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    updatePDFViewScaling()
}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    updatePDFViewScaling()
}
private func updatePDFViewScaling() {
    customView.pdfView.displayMode = .twoUp
    let scaleFactor = customView.pdfView.scaleFactorForSizeToFit
    customView.pdfView.displayMode = .twoUpContinuous
    customView.pdfView.scaleFactor = scaleFactor
    customView.pdfView.minScaleFactor = scaleFactor
    customView.pdfView.maxScaleFactor = 3
}

解决方案2:水平方向完美,垂直方向则不然。适合 2 个 PDF 的宽度,但底部被裁剪。对于 A4 文档,在大多数 iPad 上不应裁剪太多,因为它们的宽高比均为 3:4。

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUpContinuous
    view.displayDirection = .vertical
    view.minScaleFactor = view.scaleFactorForSizeToFit
    view.maxScaleFactor = 3
    return view
}()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    customView.pdfView.autoScales = true
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    customView.pdfView.autoScales = true
}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    customView.pdfView.autoScales = true
}

解决方案 3:当您用手指滚动到 PDF 内的下一页/上一页时,没有动画。

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUp
    view.displayDirection = .vertical
    view.minScaleFactor = view.scaleFactorForSizeToFit
    view.maxScaleFactor = 3
    view.autoScales = true
    return view
}()
// somewhere in view's init or wherever you want
    let upGestureRegocnizer = UISwipeGestureRecognizer(target: self, action: #selector(upSwipeAction(gestureRegonizer:)))
    upGestureRegocnizer.direction = .up
    pdfView.addGestureRecognizer(upGestureRegocnizer)
    let downGestureRegocnizer = UISwipeGestureRecognizer(target: self, action: #selector(downSwipeAction(gestureRegonizer:)))
    downGestureRegocnizer.direction = .down
    pdfView.addGestureRecognizer(downGestureRegocnizer)

@objc private func upSwipeAction(gestureRegonizer: UISwipeGestureRecognizer) {
    guard pdfView.canGoToNextPage else { return }
    pdfView.goToNextPage(self)
}
@objc private func downSwipeAction(gestureRegonizer: UISwipeGestureRecognizer) {
    guard pdfView.canGoToPreviousPage else { return }
    pdfView.goToPreviousPage(self)
}

So in the end I thought of 3 solutions, neither of them is perfect. I like solution 3 the most.


SOLUTION 1: not stable, is failing to render pdf from time to time or launches not on the 1st page. But most often works fine.

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUpContinuous
    view.autoScales = false
    view.displayDirection = .vertical
    return view
}()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    updatePDFViewScaling()
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    updatePDFViewScaling()
}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    updatePDFViewScaling()
}
private func updatePDFViewScaling() {
    customView.pdfView.displayMode = .twoUp
    let scaleFactor = customView.pdfView.scaleFactorForSizeToFit
    customView.pdfView.displayMode = .twoUpContinuous
    customView.pdfView.scaleFactor = scaleFactor
    customView.pdfView.minScaleFactor = scaleFactor
    customView.pdfView.maxScaleFactor = 3
}

SOLUTION 2: horizontally perfect, vertically not. Width of 2 PDFs fits, however the bottom is cropped. In case of A4 documents, that should crop not too much on most iPads, because all of them have the aspect ratio of 3:4.

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUpContinuous
    view.displayDirection = .vertical
    view.minScaleFactor = view.scaleFactorForSizeToFit
    view.maxScaleFactor = 3
    return view
}()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    customView.pdfView.autoScales = true
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    customView.pdfView.autoScales = true
}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    customView.pdfView.autoScales = true
}

SOLUTION 3: There is no animation when you scroll with your finger to the next/previous page INSIDE PDF.

lazy var pdfView: PDFView = {
    let view = PDFView().layoutable()
    view.displayMode = .twoUp
    view.displayDirection = .vertical
    view.minScaleFactor = view.scaleFactorForSizeToFit
    view.maxScaleFactor = 3
    view.autoScales = true
    return view
}()
// somewhere in view's init or wherever you want
    let upGestureRegocnizer = UISwipeGestureRecognizer(target: self, action: #selector(upSwipeAction(gestureRegonizer:)))
    upGestureRegocnizer.direction = .up
    pdfView.addGestureRecognizer(upGestureRegocnizer)
    let downGestureRegocnizer = UISwipeGestureRecognizer(target: self, action: #selector(downSwipeAction(gestureRegonizer:)))
    downGestureRegocnizer.direction = .down
    pdfView.addGestureRecognizer(downGestureRegocnizer)

@objc private func upSwipeAction(gestureRegonizer: UISwipeGestureRecognizer) {
    guard pdfView.canGoToNextPage else { return }
    pdfView.goToNextPage(self)
}
@objc private func downSwipeAction(gestureRegonizer: UISwipeGestureRecognizer) {
    guard pdfView.canGoToPreviousPage else { return }
    pdfView.goToPreviousPage(self)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文