zl程序教程

您现在的位置是:首页 >  前端

当前栏目

SwiftUI PDFKit如何响应翻页消息

响应消息 如何 SwiftUI 翻页
2023-09-11 14:18:31 时间

实战需求

SwiftUI PDFKit如何响应翻页消息?

实战代码

class ViewController: NSViewController {

    @IBOutlet weak var pdfView: PDFView!
    @IBOutlet weak var textView: NSTextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.pdfView.acceptsDraggedFiles = true
        observeNotifications()
    }
    
    private func observeNotifications() {
        NotificationCenter.default.addObserver(forName: NSNotification.Name.PDFViewPageChanged, object: nil, queue: nil) { (notification) in
            guard let pdfView = notification.object as? PDFView else { return }
            if let attributedString = pdfView.currentPage?.attributedString {
                self.textView.textStorage?.setAttributedString(attributedString)
            }
        }
    }

}

代码讲解

处理函数闭包,方便处理

        NotificationCenter.default.addObserver(forName: NSNotification.Name.PDFViewPageChanged, object: nil, queue: nil) { (notification) in }

技术交流

QQ:3365059189
SwiftUI技术交流QQ群:518696470