iPhone板ポコポコ

なぞると板から四角がボコボコと飛び出てくるiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SceneKit

class ViewController: UIViewController {

    weak var sceneView : SCNView?

    weak var scene : SCNScene?

    

    override func viewDidLoad() {

        super.viewDidLoad()

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

        self.setupScene()

        self.createShards()

        self.createCamera()

        self.createLight()

    }

    

    func setupScene() {

        let w = CGRectGetMaxX(self.view.bounds)

        let sv = SCNView(frame: CGRectMake(0, 0, w, w))

        sv.scene = SCNScene()

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

        self.view.addSubview(sv)

        

        self.sceneView = sv

        self.scene = sv.scene

    }

    func createShards() {

        let n = 30.0

        let dw = CGFloat(100.0 / n)

        for i in 0..<900 {

            let box = SCNBox(width: dw, height: dw, length: 5, chamferRadius: 0)

            box.firstMaterial?.diffuse.contents = UIColor.whiteColor()

            let boxNode = SCNNode(geometry: box)

            let x = Float(i % 30) * Float(dw) – 15.0 * Float(dw)

            let y = Float(i / 30) * Float(dw) – 15.0 * Float(dw)

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

            self.scene?.rootNode.addChildNode(boxNode)

        }

    }

    

    func createCamera() {

        let camera = SCNNode()

        camera.camera = SCNCamera()

        camera.rotation = SCNVector4(x: 1, y: 0, z: 0, w: 0.3)

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

        self.scene?.rootNode.addChildNode(camera)

    }

    func createLight() {

        let light = SCNLight()

        light.type = SCNLightTypeSpot

        light.color = UIColor.whiteColor()

        light.spotOuterAngle = 150

        light.castsShadow = true

        let lightNode = SCNNode()

        lightNode.light = light

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

        self.scene?.rootNode.addChildNode(lightNode)

    }

    

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

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

        self.panelMove(p)

    }

    

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

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

        self.panelMove(p)

    }

    

    func panelMove(p : CGPoint) {

        if let hits = self.sceneView?.hitTest(p, options: nil) {

            if let hit = hits.first?.node {

                

                if hit.hasActions() {

                    return;

                }

                

                hit.runAction(SCNAction.sequence([

                    SCNAction.moveTo(SCNVector3(x: hit.position.x, y: hit.position.y, z: 10), duration: 3.0),

                    SCNAction.moveTo(SCNVector3(x: hit.position.x, y: hit.position.y, z: 0), duration: 1.8)]),

                    forKey:“move”, completionHandler:{

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

                })

            }

        }

    }

    

}