2016-04-07 88 views

回答

0

从这个答案借款:https://stackoverflow.com/a/24127282/887210

核心部分你的问题是:

SCNBox(width: 1, height: 4, length: 9, chamferRadius: 0) 

这绘制一个矩形框SceneKit和UIKit。它被设置用于项目中的自定义UIViewController,但它可以很容易地适用于其他用途。

的示例代码:

override func loadView() { 
    // create a scene view with an empty scene 
    let sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300)) 
    let scene = SCNScene() 
    sceneView.scene = scene 

    // default lighting 
    sceneView.autoenablesDefaultLighting = true 

    // a camera 
    let cameraNode = SCNNode() 
    cameraNode.camera = SCNCamera() 
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 15) 
    scene.rootNode.addChildNode(cameraNode) 

    // a geometry object 
    let box = SCNBox(width: 1, height: 4, length: 9, chamferRadius: 0) 
    let boxNode = SCNNode(geometry: box) 
    scene.rootNode.addChildNode(boxNode) 

    // configure the geometry object 
    box.firstMaterial?.diffuse.contents = UIColor.redColor() 
    box.firstMaterial?.specular.contents = UIColor.whiteColor() 

    // set a rotation axis (no angle) to be able to 
    // use a nicer keypath below and avoid needing 
    // to wrap it in an NSValue 
    boxNode.rotation = SCNVector4(x: 1, y: 1, z: 0.0, w: 0.0) 

    // animate the rotation of the torus 
    let spin = CABasicAnimation(keyPath: "rotation.w") // only animate the angle 
    spin.toValue = 2.0*M_PI 
    spin.duration = 10 
    spin.repeatCount = HUGE // for infinity 
    boxNode.addAnimation(spin, forKey: "spin around") 

    view = sceneView // Set the view property to the sceneView created here. 
} 
+0

那么,如何将纳入这一个项目,是不是游戏。我是否将SceneView添加到我的视图控制器中 – Tob

+0

我已更新示例,以便您可以看到如何将其嵌入到UIViewController子类中。你基本上覆盖'loadView()'方法并将view属性设置为'sceneView'变量。 – ColGraff

+0

感谢它很好:) – Tob