SwiftUI - how to add a Scenekit Scene

前端 未结 3 532
萌比男神i
萌比男神i 2021-02-04 19:57

How can I add a Scenekit Scene to a SwiftUI view?

I tried the following Hello World, using the standard Ship Scene example...

import SwiftUI
import Scene         


        
3条回答
  •  离开以前
    2021-02-04 20:46

    In order for this to work, your SwiftUI View must conform to UIViewRepresentable. There's more info about that in Apple's tutorial: Interfacing with UIKit.

        import SwiftUI
    
    struct SwiftUIView : UIViewRepresentable {
    
        func makeUIView(context: Context) -> UIView {
            return UIStoryboard(name: "Main", bundle: Bundle.main).instantiateInitialViewController()!.view
        }
    
        func updateUIView(_ view: UIView, context: Context) {
    
        }
    }
    
    #if DEBUG
    struct SwiftUIView_Previews : PreviewProvider {
        static var previews: some View {
            SwiftUIView()
        }
    }
    #endif
    

    Note that you'll have to turn on live preview to see it working.

提交回复
热议问题