gpt4 book ai didi

ios - 如何将 SCNView 更改回 UIView?

转载 作者:行者123 更新时间:2023-11-29 05:54:13 24 4
gpt4 key购买 nike

我正在尝试使用 Xcode 游戏示例中的代码作为 Swift Playground。该代码在 Xcode 版本中完美运行,但在 Playground 中,我收到错误:

Could not cast value of type 'UIView' (0x114debe38) to 'SCNView' (0x12521d3d0).

类的类型设置为UIViewController类型,所以我不确定为什么这仅在 Playground 上不起作用。我在应用程序和 Playground 中都有相同的文件。

我已经看过this question ,但该方法似乎已经内置了。

如果它是 SCNView,我还尝试将其转换回 UIView,并且我还尝试创建一个新 View ,将 subview 添加为 SCNView,然后将最终 View 设置为新 View 。我的尝试都没有成功。

class GameScene: UIViewController {

let finalView = UIView()

override func viewDidLoad() {
super.viewDidLoad()

// create a new scene
let scene = SCNScene(named: "art.scnassets/ship.scn")!

// create and add a camera to the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)

// place the camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
cameraNode.rotation = SCNVector4(0, 0, 0, 30)

// create and add a light to the scene
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = .omni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)

// create and add an ambient light to the scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = .ambient
ambientLightNode.light!.color = UIColor.darkGray
scene.rootNode.addChildNode(ambientLightNode)

// retrieve the shark node
let shark = scene.rootNode.childNode(withName: "ship", recursively: true)!

// animate the 3d object
//shark.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)))

// retrieve the SCNView
let scnView = self.view as! SCNView

// set the scene to the view
scnView.scene = scene

// allows the user to manipulate the camera
scnView.allowsCameraControl = true

// show statistics such as fps and timing information
scnView.showsStatistics = true

// configure the view
scnView.backgroundColor = UIColor.black

// add a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
scnView.addGestureRecognizer(tapGesture)
}

@objc
func handleTap(_ gestureRecognize: UIGestureRecognizer) {
// retrieve the SCNView
let scnView = self.view as! SCNView

finalView.addSubview(scnView)

// check what nodes are tapped
let p = gestureRecognize.location(in: scnView)
let hitResults = scnView.hitTest(p, options: [:])
// check that we clicked on at least one object
if hitResults.count > 0 {
// retrieved the first clicked object
let result = hitResults[0]

// get its material
let material = result.node.geometry!.firstMaterial!

// highlight it
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5

// on completion - unhighlight
SCNTransaction.completionBlock = {
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5

material.emission.contents = UIColor.black

SCNTransaction.commit()
}

material.emission.contents = UIColor.red

SCNTransaction.commit()
}

}

override var shouldAutorotate: Bool {
return true
}

override var prefersStatusBarHidden: Bool {
return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .pad {
return .allButUpsideDown
} else {
return .all
}
}
}

PlaygroundSupport.PlaygroundPage.current.liveView = GameScene()

这应该显示一个 3D 模型和一个可以通过移动设备上的触摸来平移它的相机。

最佳答案

在您的示例中,UIViewController View 可能在界面生成器中设置为 SCNView。如果您不使用 Nib ,您可以通过重写 loadView 来完成此操作。

  override func loadView() {
let scnView = SCNView(frame: UIScreen.main.bounds, options: nil)
self.view = scnView
}

关于ios - 如何将 SCNView 更改回 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55322752/

24 4 0