Swift 如何知道何时使用PDFKit呈现PDF
我试图在屏幕上尚未显示PDF插件时显示加载。问题是,我的加载总是在文档呈现之前停止,有时它不会花费2或3秒,我需要知道PDF何时呈现以停止activyIndicator。可以使用PDFKIT吗?我的代码:Swift 如何知道何时使用PDFKit呈现PDF,swift,pdfkit,Swift,Pdfkit,我试图在屏幕上尚未显示PDF插件时显示加载。问题是,我的加载总是在文档呈现之前停止,有时它不会花费2或3秒,我需要知道PDF何时呈现以停止activyIndicator。可以使用PDFKIT吗?我的代码: class PDFViewController: URLSessionDownloadDelegate { @IBOutlet weak var pdfView: PDFView! var pdfDocument: PDFDocument! override fun
class PDFViewController: URLSessionDownloadDelegate {
@IBOutlet weak var pdfView: PDFView!
var pdfDocument: PDFDocument!
override func viewDidLoad() {
super.viewDidLoad()
setupPDFView()
setupNavigationBar()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.startLoading()
}
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadPDF()
}
private func loadPDF() {
DispatchQueue.main.async {
guard let url = URL(string: self.viewModel.pdfURL) else {
self.showAlert(with: self.viewModel.strings.invalidInvoice)
return
}
let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}
}
private func setupPDFView() {
self.pdfView.displayMode = .singlePageContinuous
self.pdfView.autoScales = true
}
func startLoading() {
guard let window = UIApplication.shared.keyWindow,
!window.subviews.contains(where: { $0 is LoadingView }) else { return }
let loadingView = LoadingView(frame: window.bounds)
window.addSubview(loadingView)
Thread.performUIUpdate {
loadingView.startAnimation()
}
}
func stopLoading() {
guard let window = UIApplication.shared.keyWindow,
let view = window.subviews.first(where: { $0 is LoadingView }),
let loadingView = view as? LoadingView else { return }
Thread.performUIUpdate {
loadingView.stopAnimation()
loadingView.removeFromSuperview()
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
DispatchQueue.main.async {
self.pdfDocument = PDFDocument(url: location)
if let pdfDocument = self.pdfDocument {
self.pdfView.document = pdfDocument
}
self?.stopLoading()
}
}
}
这似乎是一个非常糟糕的主意:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
DispatchQueue.main.async {
self.pdfDocument = PDFDocument(url: location)
问题是,当您离开当前线程,异步代码启动时,方法完成,位于位置的临时文档可能会被销毁。我建议这样写:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
let pdf = PDFDocument(url: location)
DispatchQueue.main.async {
self.pdfDocument = pdf
我并不是说这会解决你的问题,但这似乎比你现在所做的要危险得多
至于您的实际问题,我建议您注册一个通知,例如,并查看它是否在正确的时间到达。这似乎是一个非常糟糕的主意:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
DispatchQueue.main.async {
self.pdfDocument = PDFDocument(url: location)
问题是,当您离开当前线程,异步代码启动时,方法完成,位于位置的临时文档可能会被销毁。我建议这样写:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
let pdf = PDFDocument(url: location)
DispatchQueue.main.async {
self.pdfDocument = pdf
我并不是说这会解决你的问题,但这似乎比你现在所做的要危险得多
至于您的实际问题,我建议您注册一个通知,例如,并查看它是否在正确的时间到达。您必须观察PDFviewVisiblePagesAnged通知要知道PDFView页面何时可见,请在方法中添加您的代码
NotificationCenter.default.addObserver(self, selector: #selector(pdfViewVisiblePagesChanged(sender:)), name: .PDFViewVisiblePagesChanged, object: nil)
@objc func pdfViewVisiblePagesChanged(sender: Notification) {
//Add your code here
}
您必须观察PDFViewVisiblePagesChanged通知,要知道PDFView页面何时可见,请在方法中添加代码
NotificationCenter.default.addObserver(self, selector: #selector(pdfViewVisiblePagesChanged(sender:)), name: .PDFViewVisiblePagesChanged, object: nil)
@objc func pdfViewVisiblePagesChanged(sender: Notification) {
//Add your code here
}
请不要发布不完整的代码。如果self.startLoading()
相关,则显示startLoading
的定义。别让我们去猜它是干什么的,我的错。我刚加了密码好吧,我明白了。那么2秒或3秒的延迟到底在哪里呢?(我有点惊讶你的代码居然能工作,因为你没有足够快地获取下载的文档,但这可能是另一个问题。)现在我正在使用urlSession来停止loadingView。当我调用self?时。停止加载PDF已下载,并且pdfView.document已设置。但我称之为停止加载,加载停止。。。而且pdf插件还没有显示在屏幕上。有时实际显示需要2或3秒,有时调用stopLoading时会精确显示。顺便说一句,我忘了添加stopLoading定义,我正在添加,但它基本上停止了加载视图。请不要发布不完整的代码。如果self.startLoading()
相关,则显示startLoading
的定义。别让我们去猜它是干什么的,我的错。我刚加了密码好吧,我明白了。那么2秒或3秒的延迟到底在哪里呢?(我有点惊讶你的代码居然能工作,因为你没有足够快地获取下载的文档,但这可能是另一个问题。)现在我正在使用urlSession来停止loadingView。当我调用self?时。停止加载PDF已下载,并且pdfView.document已设置。但我称之为停止加载,加载停止。。。而且pdf插件还没有显示在屏幕上。有时实际显示需要2或3秒,有时调用stopLoading时会精确显示。顺便说一句,我忘了添加stopLoading定义,我正在添加,但它基本上停止了加载视图。