Last active
August 18, 2017 14:12
-
-
Save kravik/69247992919bfc1f569f2209cf3668ae to your computer and use it in GitHub Desktop.
arkit_distance.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(sceneView) | |
view.addSubview(infoLabel) | |
//1 | |
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap)) | |
tapRecognizer.numberOfTapsRequired = 1 | |
sceneView.addGestureRecognizer(tapRecognizer) | |
} | |
// MARK: Gesture handlers | |
@objc func handleTap(sender: UITapGestureRecognizer) { | |
//2 | |
let tapLocation = sender.location(in: sceneView) | |
//3 | |
let hitTestResults = sceneView.hitTest(tapLocation, types: .featurePoint) | |
if let result = hitTestResults.first { | |
//4 | |
let position = SCNVector3.positionFrom(matrix: result.worldTransform) | |
//5 | |
let sphere = SphereNode(position: position) | |
//6 | |
sceneView.scene.rootNode.addChildNode(sphere) | |
let lastNode = nodes.last | |
nodes.append(sphere) | |
if lastNode != nil { | |
//7 | |
let distance = lastNode!.position.distance(to: sphere.position) | |
infoLabel.text = String(format: "Distance: %.2f meters", distance) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment