实战需求
SwiftUI PDFKit如何响应翻页消息?
实战代码
class ViewController: NSViewController {@IBOutlet weak var pdfView: PDFView!@IBOutlet weak var textView: NSTextView!override func viewDidLoad() {super.viewDidLoad()self.pdfView.acceptsDraggedFiles = trueobserveNotifications()}private func observeNotifications() {NotificationCenter.default.addObserver(forName: NSNotification.Name.PDFViewPageChanged, object: nil, queue: nil) { (notification) inguard 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