【问题标题】:How to add 3D shapes in Swift UI?如何在 Swiftui 中添加 3D 形状?
【发布时间】:2020-03-25 16:19:02
【问题描述】:

我想知道如何在 swift UI 中添加 3D 形状(例如球体)

我尝试添加一个场景并在 swift UI 中使用它,但我收到一条错误消息

//SceneKit

class myscene: SCNScene{
 override init(){
     super.init()
 }
 required init?(coder: NSCoder) {
     fatalError("init(coder: ) has not been implemented")
 }
}

//Swift UI 

struct ContentView: View {
 var body: some View {

     let sphere = SCNSphere(radius: 2.0)
     sphere.firstMaterial?.diffuse.contents = UIColor.blue
     let spherenode = SCNNode(geometry: sphere)
     spherenode.position = SCNVector3(x: 0.0, y: 3.0, z: 0.0)
 }
}

错误消息位于var body: some View { 行,内容如下:

Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type

请帮我解决这个问题......

【问题讨论】:

    标签: swift scenekit swiftui swift-playground swift5


    【解决方案1】:

    这是演示如何使用球体设置 SceneKit 场景的最简单代码。希望对您有所帮助。

    import SwiftUI
    import SceneKit
    
    struct SceneKitView: UIViewRepresentable {
        func makeUIView(context: UIViewRepresentableContext<SceneKitView>) -> SCNView {
            let sceneView = SCNView()
            sceneView.scene = SCNScene()
            sceneView.allowsCameraControl = true
            sceneView.autoenablesDefaultLighting = true
            sceneView.backgroundColor = UIColor.black
    
            let sphere = SCNSphere(radius: 2.0)
            sphere.firstMaterial?.diffuse.contents = UIColor.blue
            let spherenode = SCNNode(geometry: sphere)
            spherenode.position = SCNVector3(x: 0.0, y: 3.0, z: 0.0)
    
            sceneView.scene?.rootNode.addChildNode(spherenode)
            return sceneView
        }
    
        func updateUIView(_ uiView: SCNView, context: UIViewRepresentableContext<SceneKitView>) {
    
        }
    
        typealias UIViewType = SCNView
    }
    
    struct DemoSceneKit: View {
        var body: some View {
            SceneKitView()
        }
    }
    
    struct DemoSceneKit_Previews: PreviewProvider {
        static var previews: some View {
            DemoSceneKit()
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2017-05-12
      • 2017-10-21
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多