Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // BoxWalk
- //
- // Created by MizushimaYusuke on 2/6/16.
- // Copyright © 2016 MizushimaYusuke. All rights reserved.
- //
- import UIKit
- import SceneKit
- class ViewController: UIViewController {
- weak var sceneView : SCNView?
- class Walk {
- var count = 0
- var positions = [SCNVector3(0, 0, 0), SCNVector3(x: -0.5, y: 0, z: 0.5), SCNVector3(x: -1.0, y: 0, z: 0), SCNVector3(x: -0.5, y: 0, z:-0.5)]
- var pivots = [SCNMatrix4MakeTranslation(0, -0.5, 0.5), SCNMatrix4MakeTranslation(-0.5, -0.5, 0), SCNMatrix4MakeTranslation(0, -0.5, -0.5), SCNMatrix4MakeTranslation(0.5, -0.5, 0)]
- var direction = [SCNVector3(x: 1, y: 0, z: 0), SCNVector3(x: 0, y: 0, z: 1), SCNVector3(x: -1, y: 0, z: 0), SCNVector3(x: 0, y: 0, z: -1)]
- func rotate(box : SCNNode) {
- box.runAction(SCNAction.rotateByAngle(CGFloat(M_PI) * 0.5, aroundAxis: direction[count], duration: 0.5), completionHandler: {
- self.count = ++self.count % 4
- box.transform = SCNMatrix4Identity
- box.pivot = self.pivots[self.count]
- box.position = self.positions[self.count]
- })
- }
- }
- var walk = Walk()
- override func viewDidLoad() {
- super.viewDidLoad()
- setupScene()
- createBase()
- createBox()
- createButton()
- }
- func setupScene() {
- let sv = SCNView(frame: view.bounds)
- sv.backgroundColor = UIColor.lightGrayColor()
- sv.scene = SCNScene()
- sv.autoenablesDefaultLighting = true
- sv.allowsCameraControl = true
- view.addSubview(sv)
- sceneView = sv
- }
- func createBase() {
- let base = SCNBox(width: 10, height: 1, length: 10, chamferRadius: 0)
- base.firstMaterial?.diffuse.contents = UIColor.brownColor()
- let node = SCNNode(geometry: base)
- node.position = SCNVector3(0, -0.5, 0)
- sceneView?.scene?.rootNode.addChildNode(node)
- }
- func createBox() {
- let box = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
- box.firstMaterial?.diffuse.contents = UIColor.yellowColor()
- let node = SCNNode(geometry: box)
- node.pivot = SCNMatrix4MakeTranslation(0, -0.5, 0.5)
- node.name = "box"
- sceneView?.scene?.rootNode.addChildNode(node)
- }
- func createButton() {
- let btn = UIButton(type: .System)
- btn.setTitle("Turn", forState: .Normal)
- btn.titleLabel?.font = UIFont.systemFontOfSize(40)
- btn.sizeToFit()
- btn.center = CGPoint(x: CGRectGetMaxX(view.bounds) - 150, y: CGRectGetMidY(view.bounds) - 150)
- view.addSubview(btn)
- btn.addTarget(self, action: "turn", forControlEvents: .TouchUpInside)
- }
- func turn() {
- if let box = sceneView?.scene?.rootNode.childNodeWithName("box", recursively: false) {
- walk.rotate(box)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement