繁体   English   中英

在 SwiftUI UIViewRepresentable 上刷新 PDFViewer

[英]Refreshing PDFViewer on SwiftUI UIViewRepresentable

我创建了一个 SwiftUI 包装器,用于创建和显示 PDF。 我有几个功能,output 和新的 pdf 作为数据。 我绑定了我的 PDFViewer,但它没有按预期工作。 当我想刷新视图(例如我添加了新文本,因此绑定数据更改)而不调用“updateUIView”时,挑战就来了。 我想在不调用 updateUIView 的情况下解决它,因为如果可能的话,我不想再次创建 PDFDocument(data: data) 。

  • 我研究了代表,没有发现任何“更新”或类似的 function。
  • 我也试过 layoutDocumentView 没有成功
  • 想要一个无需再次创建文档即可刷新视图的解决方案
  • 如果可能的话,还可以以更好的方式访问 currentPage(现在我正在使用通知)
struct PDFViewer: UIViewRepresentable {
    typealias UIViewType = PDFView
    
    @Binding var data: Data
    @Binding var currentPageNumber: Int?
    
    var pdfView: PDFView
    let singlePage: Bool
    
    init(pdfView: PDFView, data: Binding<Data>, singlePage: Bool = false, currentPage: Binding<Int?>) {
        self.pdfView = pdfView
        self._data = data
        self.singlePage = singlePage
        self._currentPageNumber = currentPage
    }
    
    func makeUIView(context: UIViewRepresentableContext<PDFViewer>) -> UIViewType {
        pdfView.autoScales = true
        if singlePage {
            pdfView.displayMode = .singlePage
        }
        pdfView.delegate = context.coordinator
        pdfView.document = PDFDocument(data: data) // <- DO NOT REFRESH EVEN IF DATA CHANGES
        
        NotificationCenter.default.addObserver(forName: .PDFViewSelectionChanged, object: nil, queue: nil) { (notification) in
            DispatchQueue.main.async {
                let newPage = (pdfView.currentPage?.pageRef!.pageNumber)!
                print(newPage)
                if currentPageNumber != newPage {
                    currentPageNumber = newPage
                }
            }
        }
        return pdfView
    }
    
    func updateUIView(_ pdfView: UIViewType, context _: UIViewRepresentableContext<PDFViewer>) {
////        let newPDFDoc = PDFDocument(data: data) <---- DO NOT WANT TO CREATE IT AGAIN
//        if pdfView.document?.dataRepresentation() != newPDFDoc?.dataRepresentation() {
////            pdfView.document = newPDFDoc
////            pdfView.go(to: pdfView.currentPage!)
//        }
    }
    
    class Coordinator: NSObject, PDFViewDelegate, UIGestureRecognizerDelegate {
        var parent: PDFViewer
        
        init(_ parent: PDFViewer) {
            self.parent = parent
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
}

您可以尝试类似这种方法的非常简单的方法,即使数据更改也不刷新视图:

@State private var firstTimeOnly = true

....

func updateUIView(_ pdfView: UIViewType, context _: UIViewRepresentableContext<PDFViewer>) {
    if self.firstTimeOnly {
        self.firstTimeOnly = false
        let newPDFDoc = PDFDocument(data: data) // <---- DO NOT WANT TO CREATE IT AGAIN
        if pdfView.document?.dataRepresentation() != newPDFDoc?.dataRepresentation() {
            pdfView.document = newPDFDoc
            pdfView.go(to: pdfView.currentPage!)
        }
    }
}

同样在makeUIView中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM