iPhone日時計

おひさまマークを動かすと影がうごいて時計みたい。なiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var scene : SCNScene?

    weak var sceneVeiw : SCNView?

    weak var sun : UIView?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        // scene kit

        self.setupScene()

        self.createGround()

        self.createPole()

        self.createCamera()

        self.createLight()

        

        // normal view

        self.view.backgroundColor = UIColor(hue: 0.6, saturation: 0.8, brightness: 1, alpha: 1)

        self.createSunController()

    }

    

    func setupScene() {

        var w = CGRectGetMaxX(self.view.bounds);

        var sv = SCNView(frame: CGRect(x: 0, y: 0, width: w, height: w))

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

        sv.scene = SCNScene()

        self.view.addSubview(sv)

        

        self.scene = sv.scene

        self.sceneVeiw = sv

    }

    

    func createGround() {

        var ground = SCNBox(width: 20, height: 1, length: 20, chamferRadius: 0)

        ground.firstMaterial?.diffuse.contents = UIColor(hue: 0.4, saturation: 0.4, brightness: 0.7, alpha: 1)

        var groundNode = SCNNode(geometry: ground)

        groundNode.name = “ground”

        self.scene?.rootNode.addChildNode(groundNode)

    }

    func createPole() {

        var pole = SCNCylinder(radius: 0.4, height: 6)

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

        var poleNode = SCNNode(geometry: pole)

        poleNode.position = SCNVector3(x: 0, y: 3, z: 0)

        self.scene?.rootNode.addChildNode(poleNode)

    }

    

    func createCamera() {

        var camera = SCNNode()

        camera.camera = SCNCamera()

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

        self.scene?.rootNode.addChildNode(camera)

        

        if let target = self.scene?.rootNode.childNodeWithName(“ground”, recursively: false) {

            camera.constraints = [SCNLookAtConstraint(target:target)]

        }

    }

    

    func createLight() {

        var light = SCNLight()

        light.type = SCNLightTypeSpot

        var lightNode = SCNNode()

        lightNode.name = “light”

        lightNode.light = light

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

        if let target = self.scene?.rootNode.childNodeWithName(“ground”, recursively: false) {

            lightNode.constraints = [SCNLookAtConstraint(target: target)]

        }

        lightNode.light?.castsShadow = true

        self.scene?.rootNode.addChildNode(lightNode)

    }

    

    func createSunController()

    {

        var sun = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))

        sun.layer.cornerRadius = 15

        sun.center = CGPoint(x: CGRectGetMidX(self.view.bounds), y: CGRectGetMaxY(self.view.bounds) * 3.0 / 4.0)

        sun.backgroundColor = UIColor(hue: 0.02, saturation: 0.7, brightness: 1, alpha: 1)

        for i in 05 {

            var angle = Double(i) * (M_PI/3.0)

            var x = 15.0 * cos(angle) + 15.0

            var y = 15.0 * sin(angle) + 15.0

            var f = UIView(frame: CGRect(x: 0, y: 0, width: 6, height: 6))

            f.center = CGPoint(x: x, y: y)

            f.backgroundColor = sun.backgroundColor

            f.transform = CGAffineTransformMakeRotation(CGFloat(angle+M_PI/4.0))

            sun.addSubview(f)

        }

        self.view.addSubview(sun)

        

        self.sun = sun;

    }

    

    

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

        var p  = touches.anyObject()!.locationInView(self.view)

        self.sun?.center = p

        

        var o = self.sceneVeiw!.projectPoint(SCNVector3Zero)

        var adjustY : Float = 300.0

        var vector = self.sceneVeiw!.unprojectPoint(SCNVector3(x: Float(p.x), y: Float(p.y) – adjustY, z: o.z))

        

        var light = self.scene?.rootNode.childNodeWithName(“light”, recursively: false)

        light?.position = SCNVector3(x: 5.0 * vector.x, y: 50, z: –5.0 * vector.y)

    }

    

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}