iPhoneカラーねじねじ

カラフルなねじねじを表示するiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController, SCNSceneRendererDelegate {

    weak var sceneView : SCNView?

    var count = 0

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createFlower()

        createCamera()

    }

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

        sv.backgroundColor = UIColor(hue: 0.1, saturation: 0.1, brightness: 1, alpha: 1)

        sv.scene = SCNScene()

        sv.delegate = self

        sv.allowsCameraControl = true

        view.addSubview(sv)

        

        sceneView = sv

    }

    

    func createFlower() {

        let flower = SCNNode()

        flower.name = “flower”

        self.sceneView?.scene?.rootNode.addChildNode(flower)

        

        let dw = M_PI / 2.5

        [Int](04).each { i in

            let s = SCNSphere(radius: 0.8)

            s.firstMaterial?.diffuse.contents = UIColor(hue: CGFloat(i) * 0.2, saturation: 0.2, brightness: 1, alpha: 1)

            let snode = SCNNode(geometry: s)

            let x = 2.0 * cos(dw * Double(i))

            let z = 2.0 * sin(dw * Double(i))

            snode.pivot = SCNMatrix4MakeTranslation(Float(x), 0, Float(z))

            flower.addChildNode(snode)

            snode.runAction(SCNAction.repeatActionForever(SCNAction.rotateByAngle(CGFloat(0.1 * M_PI), aroundAxis: SCNVector3(x: 0, y: 1, z: 0), duration: 0.03)))

        }

        

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

        camera.position = SCNVector3(x: 20, y: 5, z: 30)

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

    }

    

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        if self.count == 0 {

            self.count = 1

        }

    }

    

    func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {

        

        let dw = M_PI * 0.01

        if let flower = sceneView?.scene?.rootNode.childNodeWithName(“flower”, recursively: false) {

            if self.count > 0 && self.count < 300 {

                

                let w = dw * Double(count) % (2.0 * M_PI)

                let x = Float(count) * 0.1

                let y = 8.0 * sin(w)

                flower.position = SCNVector3(x: Float(x), y: Float(y), z: 0)

                flower.rotation = SCNVector4(x: 0, y: 0, z: 1, w: w < M_PI ? –Float(w) : Float(w))

                

                if self.count % 3 == 0 {

                    for c in flower.childNodes {

                        let n = SCNNode(geometry: (c as! SCNNode).geometry!)

                        n.pivot = c.pivot

                        n.rotation = c.presentationNode().rotation

                        n.position = flower.convertPosition(n.position, toNode: self.sceneView?.scene?.rootNode)

                        self.sceneView?.scene?.rootNode.addChildNode(n)

                    }

                }

                

                ++self.count

            }

        }

    }

}

extension Array {

    func each(doit:T -> Void) { for i in self {doit(i)}}

}