首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >刷新PDFViewer on SwiftUI UIViewRepresentable

刷新PDFViewer on SwiftUI UIViewRepresentable
EN

Stack Overflow用户
提问于 2021-12-21 03:59:42
回答 1查看 267关注 0票数 0

我已经创建了一个用于创建和显示PDF的SwiftUI包装器。我有两个功能,输出新的pdf作为数据。我有一个绑定到我的PDFViewer,但它并不像预期的那样工作。当我想要刷新视图时(例如,我添加了新文本,所以绑定数据发生了变化),而不调用“updateUIView”,就会遇到挑战。我想在不调用updateUIView的情况下解决这个问题,因为如果可能的话,我不想再次创建PDFDocument(data: data)。

我已经研究过代表,没有发现任何‘更新’或类似的功能。我还尝试了

  • ,但没有成功,
  • 想要一种不需要再创建文档就刷新视图的解决方案,如果可能的话,
  • 也可以更好地访问currentPage (现在我正在使用
  • )。

代码语言:javascript
复制
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)
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-12-21 04:22:08

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

代码语言:javascript
复制
@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中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70430526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档