iPhoneハト時計

時計から鳥がピッポピッポするかんじでiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createClock()

        createHands()

        createBird()

        createCamera()

    }

    func setupScene() {

        let sv = SCNView(frame: view.bounds)

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

        sv.scene = SCNScene()

        sv.allowsCameraControl = true

        sv.autoenablesDefaultLighting = true

        view.addSubview(sv)

        

        sceneView = sv

    }

    

    func createClock() {

        let path = UIBezierPath(arcCenter: CGPointZero, radius: 5, startAngle: 0, endAngle: 2.0 * CGFloat(M_PI), clockwise: false)

        path.appendPath(UIBezierPath(arcCenter: CGPoint(x: 0, y: 3), radius: 1, startAngle: 0, endAngle: 2.0 * CGFloat(M_PI), clockwise: false))

        path.usesEvenOddFillRule = true

        path.flatness = 0.01

        

        let plate = SCNShape(path: path, extrusionDepth: 0.3)

        plate.firstMaterial?.diffuse.contents = UIColor(white: 0.9, alpha: 1)

        let plateNode = SCNNode(geometry: plate)

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

    }

    

    func createHands() {

        let lhand = SCNBox(width: 0.2, height: 4, length: 0.2, chamferRadius: 0.2)

        lhand.firstMaterial?.diffuse.contents = UIColor(hue: 0.25, saturation: 0.8, brightness: 0.4, alpha: 1)

        let lhandNode = SCNNode(geometry: lhand)

        lhandNode.pivot = SCNMatrix4MakeTranslation(0, –1.9, 0)

        lhandNode.position = SCNVector3(x: 0, y: 0, z: 0.8)

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

        

        let shand = SCNBox(width: 0.2, height: 2, length: 0.2, chamferRadius: 0.2)

        shand.firstMaterial?.diffuse.contents = UIColor(hue: 0.05, saturation: 0.8, brightness: 0.6, alpha: 1)

        let shandNode = SCNNode(geometry: shand)

        shandNode.pivot = SCNMatrix4MakeTranslation(0, –0.9, 0)

        shandNode.position = SCNVector3(x: 0, y: 0, z: 0.9)

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

        

        lhandNode.runAction(SCNAction.repeatActionForever(SCNAction.rotateByAngle(CGFloat(M_PI), aroundAxis: SCNVector3(x: 0, y: 0, z: 1), duration: 2.0)))

        

        shandNode.runAction(SCNAction.repeatActionForever(SCNAction.rotateByAngle(CGFloat(M_PI), aroundAxis: SCNVector3(x: 0, y: 0, z: 1), duration: 12.0)))

    }

    

    func createBird() {

        let bird = SCNNode()

        bird.name = “bird”

        

        let body = SCNSphere(radius: 0.8)

        body.firstMaterial?.diffuse.contents = UIColor(hue: 0.125, saturation: 0.3, brightness: 1, alpha: 1)

        let bodyNode = SCNNode(geometry: body)

        bird.addChildNode(bodyNode)

        

        for i in 01 {

            let eye = SCNSphere(radius: 0.1)

            eye.firstMaterial?.diffuse.contents = UIColor.blackColor()

            let eyeNode = SCNNode(geometry: eye)

            eyeNode.position = SCNVector3(x: i==0 ? 0.4 : –0.4, y: 0.2, z: 0.7)

            bird.addChildNode(eyeNode)

        }

        

        let m = SCNCone(topRadius: 0.05, bottomRadius: 0.2, height: 0.5)

        m.firstMaterial?.diffuse.contents = UIColor.yellowColor()

        m.firstMaterial?.specular.contents = UIColor.whiteColor()

        let mNode = SCNNode(geometry: m)

        mNode.position = SCNVector3(x: 0, y: –0.3, z: 1)

        mNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: Float(M_PI) * 0.6)

        bird.addChildNode(mNode)

        bird.pivot = SCNMatrix4MakeTranslation(0, –3, 0)

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

        

        bird.runAction(SCNAction.rotateToAxisAngle(SCNVector4(x: 1, y: 0, z: 0, w: –Float(M_PI) * 0.5), duration: 0.6))

    }

    

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

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

            bird.runAction(SCNAction.sequence([

                    SCNAction.rotateToAxisAngle(SCNVector4(x: 1, y: 0, z: 0, w: Float(M_PI) * 0.2) , duration: 0.3),

                    SCNAction.rotateToAxisAngle(SCNVector4(x: 1, y: 0, z: 0, w: –Float(M_PI) * 0.5), duration: 0.6)

            ]))

        }

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

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

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

    }

}