2017-05-12 62 views
0

我想訪問childnode:boxNode,然後使用來自平移手勢識別器的輸入旋轉節點。我應該如何訪問這個節點,以便操縱它?我是否應該在全局範圍內聲明多維數據集節點並進行修改?我對Swift很陌生,所以我很抱歉,如果這段代碼看起來馬虎。我想在我的panGesture()函數中添加旋轉動作。謝謝!訪問幾何子節點場景包

import UIKit 
import SceneKit 

class GameViewController: UIViewController { 

var scnView: SCNView! 
var scnScene: SCNScene! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    setupView() 
    setupScene() 
} 

// override var shouldAutorotate: Bool { 
// return true 
// } 
// 
// override var prefersStatusBarHidden: Bool { 
// return true 
// } 

func setupView() { 
    scnView = self.view as! SCNView 
} 

func setupScene() { 
    scnScene = SCNScene() 
    scnView.scene = scnScene 
    scnView.backgroundColor = UIColor.blueColor() 
    initCube() 
    initLight() 
    initCamera() 
} 

func initCube() { 
    var cubeGeometry:SCNGeometry 
    cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0) 
    let boxNode = SCNNode(geometry: cubeGeometry) 
    scnScene.rootNode.addChildNode(boxNode) 
} 

func initLight() { 
    let light = SCNLight() 
    light.type = SCNLightTypeAmbient 
    let lightNode = SCNNode() 
    lightNode.light = light 
    light.castsShadow = false 
    lightNode.position = SCNVector3(x: 1.5, y: 1.5, z: 1.5) 
    scnScene.rootNode.addChildNode(lightNode) 
} 

func initCamera() { 
    let camera = SCNCamera() 
    let cameraNode = SCNNode() 
    cameraNode.camera = camera 
    cameraNode.position = SCNVector3(x: 0.0, y: 0.0, z: 5.0) 
    scnScene.rootNode.addChildNode(cameraNode) 
} 

func initRecognizer() { 
    let panTestRecognizer = UIPanGestureRecognizer(target: self, action: #selector(GameViewController.panGesture(_:))) 
    scnView.addGestureRecognizer(panTestRecognizer) 
} 

//TAKE GESTURE INPUT AND ROTATE CUBE ACCORDINGLY 
func panGesture(sender: UIPanGestureRecognizer) { 
    //let translation = sender.translationInView(sender.view!) 
} 

}

回答

0

,首先你應該更新您的Xcode。它看起來像是你正在使用的較老的Swift版本。 SCNLightTypeAmbient是斯威夫特3

.ambient所以,你應該走的路是給你的節點的名稱來識別它們:

myChildNode.name = "FooChildBar"

,然後您可以在父節點上調用

let theNodeYouAreLookingFor = parentNode.childNode(withName: "FooChildBar", recursively: true)

你也可以在你的場景的根節點上使用它

let theNodeYouAreLookingFor = scene.rootNode.childNode(withName: "FooChildBar", recursively: true)

它將查看場景中的所有節點並返回給出名稱的節點。