2

注意:这个问题暂时还没有解决;标记的答案提供了一个很好的解决方法 - 它在应用程序仍处于打开状态时工作。仍然欢迎回答!

背景

我目前正在开发一个包含 full-screen 的应用程序,PDFView我希望程序在视图被关闭之前记住文档中的位置,以便用户可以在他们离开的地方找到。

执行

该应用程序的简化版本可以理解为使用PDFKit.PDFView. 故事板由一个连接到 PDFView 类的 UIView 组成DocumentView(符合UIViewController)。视图通过以下过程初始化:

viewDidLoad

let PDF: PDFDocument = GetPDFFromServer()
DocumentView.document = PDF!
DocumentView.autoScales = true
... (other settings)

// Set PDF Destination
let Destination: PDFDestination = GetStoredDestination()
// Code with issues
DocumentView.go(to: Destination)

viewWillDisappear

StoreDestination(DocumentView.currentDestination)

问题与测试

我意识到代码没有按预期工作;视图不会返回到之前的位置。

通过调试,我意识到这可能是由于 and 的行为不一致造成DocumentView.go(to destination: PDFDestination)DocumentView.currentDestination

为确保在存储位置时不会因错误而引入错误,以下代码用于验证问题,并带有多页文档:

viewDidLoad

Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
    DispatchQueue.main.async {
    
self.DocumentView.go(to:self.DocumentView.currentDestination!)
    }

})

预期和观察到的行为

预期:文档的位置不应该改变 - 代码每 1 秒到达其当前目的地,这应该没有任何影响。因为“currentDestination”应该是“文档的当前目的地,每个文档”)

Observed:在执行时,页面会自发地向下滚动一个固定的偏移量。

在 iPadOS 14.5 模拟器和 iPadOS 15 iPad Air(第 4 代)上观察到了相同的结果。

可能出了什么问题?

如果有人可以提供帮助,那就太好了。

干杯,林肯

这个问题最初是在一周前发布在苹果开发者论坛上的;一个多星期没有收到任何回复,所以我想我可以在 StackOverflow <3 上试试运气

4

1 回答 1

1

我尝试 PDFView.go()了这种情况,并设法让它在某些情况下工作,但发现它在其他一些情况下失败,例如缩放文档、改变方向。

所以回到你想要达到的目标,

我目前正在开发一个由全屏 PDFView 组成的应用程序,我希望程序在视图被关闭之前记住文档中的位置,以便用户可以在他们离开的地方找到。

这可以通过不同的方法来完成。使用这种方法,您需要始终保留对PDFView您创建的引用。如果需要再次加载以前的 pdf,那么您将PDFView拥有的实例按viewController原样传递。否则,您将新的 pdf 加载到PDFView实例并将其传递给viewController.

DocumentViewController获取PDFView初始化的时间。

import UIKit
import PDFKit

protocol DocumentViewControllerDelegate: AnyObject {
    func needsContinuePDF(continuePDF: Bool)
}

class DocumentViewController: UIViewController {
    var pdfView: PDFView!
    weak var delegate: DocumentViewControllerDelegate!
    
    init(pdfView: PDFView, delegate: DocumentViewControllerDelegate){
        self.pdfView = pdfView
        self.delegate = delegate
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        super.loadView()
        self.view.backgroundColor = .white
        view.addSubview(pdfView)
        NSLayoutConstraint.activate([
            pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
            pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
        ])
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        delegate.needsContinuePDF(continuePDF: true)
    }
}

DocumentViewController您可以像下面这样初始化。MainViewController负责初始化PDFView

import UIKit
import PDFKit

class MainViewController: UIViewController {
    var pdfView: PDFView = PDFView()
    var continuePreviousPDF = false
    
    let button = UIButton(frame: .zero)
    
    override func loadView() {
        super.loadView()
        
        button.setTitle("View PDF", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.backgroundColor = .systemBlue
        button.addTarget(self, action: #selector(openDocumentView(_:)), for: .touchUpInside)
        self.view.backgroundColor = .systemGray5
        self.view.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.widthAnchor.constraint(equalToConstant: 100),
            button.heightAnchor.constraint(equalToConstant: 50),
            button.centerXAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerXAnchor),
            button.centerYAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.centerYAnchor),
        ])
    }
    
    @objc func openDocumentView(_ sender: UIButton) {
        //open a nee PDF if not continue previous one
        if !self.continuePreviousPDF {
            pdfView.autoScales = true
            pdfView.displayMode = .singlePageContinuous
            pdfView.translatesAutoresizingMaskIntoConstraints = false
            
            guard let path = Bundle.main.url(forResource: "sample copy", withExtension: "pdf") else { return }

            if let document = PDFDocument(url: path) {
                pdfView.document = document
            }
        }
        
        let documentViewController = DocumentViewController(pdfView: pdfView, delegate: self)
        self.present(documentViewController, animated: true, completion: nil)
    }
}

extension MainViewController: DocumentViewControllerDelegate {
    func needsContinuePDF(continuePDF: Bool) {
        self.continuePreviousPDF = continuePDF
    }
}

在此处输入图像描述

于 2021-09-25T17:56:13.043 回答