Skip to content

Instantly share code, notes, and snippets.

@kravik
Last active August 18, 2017 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kravik/69247992919bfc1f569f2209cf3668ae to your computer and use it in GitHub Desktop.
Save kravik/69247992919bfc1f569f2209cf3668ae to your computer and use it in GitHub Desktop.
arkit_distance.swift
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