左右からにょきっとアーチがでてきてアーケードをつくるiPhoneアプリのサンプルコード
import UIKit
import SceneKit
class ViewController: UIViewController {
weak var sceneView : SCNView?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createRoad()
createArc()
createCamera()
}
func setupScene() {
sceneView = {
let sv = SCNView(frame: self.view.bounds)
sv.scene = SCNScene()
sv.backgroundColor = UIColor(white: 0.9, alpha: 1)
sv.autoenablesDefaultLighting = true
self.view.addSubview(sv)
return sv
}()
}
func createRoad() {
let road = SCNBox(width: 20, height: 5, length: 60, chamferRadius: 0)
road.firstMaterial?.diffuse.contents = UIColor(hue: 0.15, saturation: 0.1, brightness: 0.8, alpha: 1)
let roadNode = SCNNode(geometry: road)
sceneView?.scene?.rootNode.addChildNode(roadNode)
}
func createArc() {
let arcpath = UIBezierPath(arcCenter: CGPointZero, radius: 8, startAngle: 0, endAngle: CGFloat(M_PI), clockwise: false)
arcpath.addLineToPoint(CGPoint(x: –7, y: 0))
arcpath.addArcWithCenter(CGPointZero, radius: 7, startAngle: CGFloat(M_PI), endAngle: 0, clockwise: true)
arcpath.closePath()
arcpath.flatness = 0.1
[Int](0…10).foreach { (i : Int) -> Void in
let arc = SCNShape(path: arcpath, extrusionDepth: 2)
arc.firstMaterial?.diffuse.contents = UIColor(hue: 0.15, saturation: 0.3, brightness: 0.6, alpha: 1)
let arcNode = SCNNode(geometry: arc)
arcNode.name = “arc”
arcNode.position = SCNVector3(x: 0, y: 2.4, z: 30 – 8.0 * Float(i))
self.sceneView?.scene?.rootNode.addChildNode(arcNode)
}
}
func createCamera() {
let camera = SCNNode()
camera.camera = SCNCamera()
camera.position = SCNVector3(x: 0, y: 3, z: 50)
camera.rotation = SCNVector4Make(1, 0, 0, 0.1)
sceneView?.scene?.rootNode.addChildNode(camera)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
var cnt = 0
sceneView?.scene?.rootNode.childNodes
.filter { ($0 as SCNNode).name == “arc” }
.sorted { ($0 as SCNNode).position.z < $1.position.z}
.map { (n) -> Void in
n.runAction(SCNAction.sequence([SCNAction.waitForDuration(NSTimeInterval(++cnt) * 0.2), SCNAction.rotateByAngle(cnt % 2 == 1 ? CGFloat(M_PI) : CGFloat(-M_PI), aroundAxis: SCNVector3(x: 0, y: 0, z: 1), duration: 2.0)]))}
}
}
extension Array {
func foreach(doit:(Element) -> Void) { for i in self { doit(i) } }
}