SwiftUI 中的 PDFKit .go(to:) 方法
PDFKit .go(to: ) Method in SwiftUI
你好,我想在我的 SwiftUI 应用程序的第 7 页显示一个 PDF 文件。
我还想在 UIViewRepresentable.
之外使用函数 .go(to :) 和 .currentPage()
import SwiftUI
import PDFKit
struct PDFKitView: View {
var pdf: PDF
var body: some View {
PDFKitRepresentedView(pdf)
}
}
struct PDFKitRepresentedView: UIViewRepresentable {
let pdf: PDF
init(_ pdf: PDF) {
self.pdf = pdf
}
func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
let pdfView = PDFView()
let document = PDFDocument(data: pdf.content!)
pdfView.document = document
pdfView.autoScales = true
print("pdfView.currentPage: \(String(describing: pdfView.currentPage))") // pdfView.currentPage: Optional(<PDFPage: 0x600002245420> page index 0)
print("pdfView: \(pdfView)") // pdfView: <PDFView: 0x7fb206526c20; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x600002ef4150>; layer = <CALayer: 0x60000202ddc0>>
if let myPage = document?.page(at: 7) {
pdfView.go(to: myPage)
}
print("pdfView.currentPage: \(String(describing: pdfView.currentPage))") // pdfView.currentPage: Optional(<PDFPage: 0x6000022455c0> page index 7)
print("pdfView: \(pdfView)") // pdfView: <PDFView: 0x7fb206526c20; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x600002ef4150>; layer = <CALayer: 0x60000202ddc0>>
return pdfView
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
// Update the view.
}
}
显示了 PDF 文件,但在第 1 页而不是第 7 页:/
我对它还比较陌生Swift,谁能给我提示?
现在是这样的
问题是
PDFView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
而不是
PDFView()
这里是完整的代码
import SwiftUI
import PDFKit
struct PDFKitView: View {
@State var pdf: PDF
@Binding var pageIndex: Int
var body: some View {
VStack{
PDFPreviewController(pdfX: $pdf , pageIndex: $pageIndex )
}
}
}
class PDFPreviewViewConroller: UIViewController {
public var pdfView: PDFView!
override func loadView() {
pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
self.view = pdfView
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
struct PDFPreviewController: UIViewControllerRepresentable {
@Binding var pdf: PDF
@Binding var pageIndex: Int
init(pdfX: Binding<PDF>, pageIndex: Binding<Int>) {
_pdf = pdfX
_pageIndex = pageIndex
}
func makeUIViewController(context: UIViewControllerRepresentableContext<PDFPreviewController>) -> PDFPreviewViewConroller {
return PDFPreviewViewConroller()
}
func updateUIViewController(_ uiViewController: PDFPreviewViewConroller, context: UIViewControllerRepresentableContext<PDFPreviewController>) {
uiViewController.pdfView.document = PDFDocument(data: pdf.content!)
if let myPage = uiViewController.pdfView.document?.page(at: (pageIndex)) {
uiViewController.pdfView.go(to: myPage)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(pdf: $pdf, pageIndex: $pageIndex)
}
class Coordinator: NSObject {
@Binding var pageIndex: Int
@Binding var pdf: PDF
init(pdf: Binding<PDF>, pageIndex: Binding<Int>) {
_pageIndex = pageIndex
_pdf = pdf
}
}
}
你好,我想在我的 SwiftUI 应用程序的第 7 页显示一个 PDF 文件。
我还想在 UIViewRepresentable.
之外使用函数 .go(to :) 和 .currentPage()import SwiftUI
import PDFKit
struct PDFKitView: View {
var pdf: PDF
var body: some View {
PDFKitRepresentedView(pdf)
}
}
struct PDFKitRepresentedView: UIViewRepresentable {
let pdf: PDF
init(_ pdf: PDF) {
self.pdf = pdf
}
func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
let pdfView = PDFView()
let document = PDFDocument(data: pdf.content!)
pdfView.document = document
pdfView.autoScales = true
print("pdfView.currentPage: \(String(describing: pdfView.currentPage))") // pdfView.currentPage: Optional(<PDFPage: 0x600002245420> page index 0)
print("pdfView: \(pdfView)") // pdfView: <PDFView: 0x7fb206526c20; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x600002ef4150>; layer = <CALayer: 0x60000202ddc0>>
if let myPage = document?.page(at: 7) {
pdfView.go(to: myPage)
}
print("pdfView.currentPage: \(String(describing: pdfView.currentPage))") // pdfView.currentPage: Optional(<PDFPage: 0x6000022455c0> page index 7)
print("pdfView: \(pdfView)") // pdfView: <PDFView: 0x7fb206526c20; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x600002ef4150>; layer = <CALayer: 0x60000202ddc0>>
return pdfView
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
// Update the view.
}
}
显示了 PDF 文件,但在第 1 页而不是第 7 页:/
我对它还比较陌生Swift,谁能给我提示?
现在是这样的
问题是
PDFView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
而不是
PDFView()
这里是完整的代码
import SwiftUI
import PDFKit
struct PDFKitView: View {
@State var pdf: PDF
@Binding var pageIndex: Int
var body: some View {
VStack{
PDFPreviewController(pdfX: $pdf , pageIndex: $pageIndex )
}
}
}
class PDFPreviewViewConroller: UIViewController {
public var pdfView: PDFView!
override func loadView() {
pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
self.view = pdfView
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
struct PDFPreviewController: UIViewControllerRepresentable {
@Binding var pdf: PDF
@Binding var pageIndex: Int
init(pdfX: Binding<PDF>, pageIndex: Binding<Int>) {
_pdf = pdfX
_pageIndex = pageIndex
}
func makeUIViewController(context: UIViewControllerRepresentableContext<PDFPreviewController>) -> PDFPreviewViewConroller {
return PDFPreviewViewConroller()
}
func updateUIViewController(_ uiViewController: PDFPreviewViewConroller, context: UIViewControllerRepresentableContext<PDFPreviewController>) {
uiViewController.pdfView.document = PDFDocument(data: pdf.content!)
if let myPage = uiViewController.pdfView.document?.page(at: (pageIndex)) {
uiViewController.pdfView.go(to: myPage)
}
}
func makeCoordinator() -> Coordinator {
Coordinator(pdf: $pdf, pageIndex: $pageIndex)
}
class Coordinator: NSObject {
@Binding var pageIndex: Int
@Binding var pdf: PDF
init(pdf: Binding<PDF>, pageIndex: Binding<Int>) {
_pageIndex = pageIndex
_pdf = pdf
}
}
}