iPhone線が向かってくる

線が向かってくるようなかんじのiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit;

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createLines()

        createCamera()

    }

    

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

        sv.scene = SCNScene()

        sv.backgroundColor = UIColor.blackColor()

        sv.autoenablesDefaultLighting = true

        view.addSubview(sv)

        

        sceneView = sv

    }

    func createLines() {

        let dangle = M_PI / 3.0

        for i in 06 {

            let b = SCNBox(width: 2, height: 2, length: 100, chamferRadius: 1)

            b.firstMaterial?.diffuse.contents = UIColor(hue: CGFloat(i) * 0.13, saturation: 0.5, brightness: 1, alpha: 1)

            let bNode = SCNNode(geometry: b)

            bNode.name = “line”

            bNode.position = SCNVector3(x: 0, y: 0, z: –100)

            sceneView?.scene?.rootNode.addChildNode(bNode)

            if i > 0 {

                bNode.transform = SCNMatrix4Rotate(bNode.transform, 0.1, 0, 1, 0)

                bNode.transform = SCNMatrix4Rotate(bNode.transform, Float(dangle) * Float(i), 0, 0, 1)

            }

        }

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.position = SCNVector3(x: 0, y: 0, z: 50)

        camera.camera = SCNCamera()

        camera.camera?.zFar = 200

        sceneView?.scene?.rootNode.addChildNode(camera)

    }

    

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        sceneView?.scene?.rootNode.childNodes

            .filter { ($0 as SCNNode).name == “line” }

            .map { (n : AnyObject) -> Void in

            let v = (n as SCNNode).convertPosition(SCNVector3(x: 0, y: 0, z: 300), toNode: self.sceneView?.scene?.rootNode)

            (n as SCNNode).runAction(SCNAction.moveBy(v, duration: 5.0))

        }

    }

}