I´m trying to display a Quick Look View directly - without a sheet Presenting the Reality File
. If I start the App at the first time it works fine:
when I'm switching between a TabView back to the first ModelView I don't the the reality Model anymore:
I tried
.onAppear {
PreviewController(url: self.fileUrl)
}
but this doesn't work. I also tried SceneKit
, it worked here but here I can only use usdz-Files
. If I'm Presenting the sheet with the Button it's working fine.
Here the Files:
ModelView.swift
import SwiftUI
struct ModelView: View {
let fileUrl = Bundle.main.url(forResource: "LunarRover_English", withExtension: "reality")!
@State private var showingPreview = false
var body: some View {
ScrollView{
Text("Rover")
.font(.title2)
.onAppear {
print("ARVRView-Text")
//self.showingPreview = true
//PreviewController(url: self.fileUrl)
}
ZStack(alignment: .topTrailing){
PreviewController(url: self.fileUrl)
.aspectRatio(1.20, contentMode: .fit)
Button {
self.showingPreview = true
} label: {
Image(systemName: "rectangle.expand.vertical")
}
.sheet(isPresented: $showingPreview) {
VStack(spacing: 0) {
HStack {
Button("Zurück") {
self.showingPreview = false
}
Spacer()
}
.padding()
PreviewController(url: self.fileUrl)
}
}
.foregroundColor(.black)
.imageScale(.large)
.symbolRenderingMode(.hierarchical)
.offset(x:-10, y:10)
}
}
}
}
struct ARVRView_Previews: PreviewProvider {
static var previews: some View {
ARVRView()
}
}
PreviewController.swift
import SwiftUI
import UIKit
import QuickLook
struct PreviewController: UIViewControllerRepresentable {
let url: URL
func makeUIViewController(context: Context) -> QLPreviewController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
return controller
}
func updateUIViewController(
_ uiViewController: QLPreviewController, context: Context) {}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
class Coordinator: QLPreviewControllerDataSource {
let parent: PreviewController
init(parent: PreviewController) {
self.parent = parent
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return parent.url as NSURL
}
}
}