iPhone初春2015

2015年ですよというiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    var count = 0

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        create2014()

        createCamera()

    }

    func setupScene() {

        let sv = SCNView(frame: self.view.bounds)

        sv.scene = SCNScene()

        sv.backgroundColor = UIColor(hue: 0.6, saturation: 0.3, brightness: 0.2, alpha: 1)

        sv.autoenablesDefaultLighting = true

        self.view.addSubview(sv)

        

        sceneView = sv

    }

    

    func create2014() {

        

        let buildtext = { (s : String) -> CALayer in

            let textLayer = CATextLayer()

            textLayer.backgroundColor = UIColor.whiteColor().CGColor

            textLayer.frame = CGRect(x: 0, y: 0, width: 200, height: 200)

            

            var attrs = [

                NSFontAttributeName : UIFont.systemFontOfSize(160.0),

                NSForegroundColorAttributeName :UIColor.darkGrayColor()

            ]

            textLayer.string = NSMutableAttributedString(string:s, attributes:attrs)

            textLayer.alignmentMode = kCAAlignmentCenter;

            textLayer.foregroundColor = UIColor.blackColor().CGColor

            return textLayer

        }

        

        for (n, c) in enumerate([2, 0, 1, 4]) {

            let box = SCNBox(width: 10, height: 10, length: 10, chamferRadius: 1)

            var arr = Array<SCNMaterial>()

            for j in 0..<6 {

                let m = SCNMaterial()

                if j > 0 { m.diffuse.contents = buildtext(\(c + 1)) }

                else { m.diffuse.contents = buildtext(\(c)) }

                arr.append(m)

            }

            box.materials = arr

            

            let boxNode = SCNNode(geometry: box)

            boxNode.name = “box\(n)

            boxNode.position = SCNVector3(x: –15 + 10 * Float(n), y: 0, z: 0)

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

        }

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

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

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

    }

    

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

        let box = sceneView?.scene?.rootNode.childNodeWithName(“box\(count), recursively: false)

        if count != 3 {

            box?.runAction(SCNAction.sequence([

                SCNAction.moveBy(SCNVector3(x: 0, y: 0, z: 10), duration: 0.4),

                SCNAction.waitForDuration(0.1),

                SCNAction.moveBy(SCNVector3(x: 0, y: 0, z: –10), duration: 0.4)

                ]))

        } else {

            box?.runAction(SCNAction.sequence([

                SCNAction.moveBy(SCNVector3(x: 0, y: 0, z: 10), duration: 0.4),

                SCNAction.rotateByX(CGFloat(M_PI) * 0.5, y: 0, z: 0, duration: 0.4),

                SCNAction.moveBy(SCNVector3(x: 0, y: 0, z: –10), duration: 0.4)

                ]))

        }

        

        

        ++count

    }

}