So I am new to Swift, Xcode and Arkit. I work with Xcode 9.2
I wanted to build a simple app that places an object when tapped on the screen to the position it detected a plane or point.
I Started up Xcode and wrote this short piece of code into the basic example thats there when u open up with scenekit. Code looks like this:
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
// Create a new scene
let scene = SCNScene(named: "art.scnassets/ship.scn")!
// Set the scene to the view
sceneView.scene = scene
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Run the view's session
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {return}
let result = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
guard let hitResult = result.last else {return}
let hitTransform = SCNMatrix4.init(hitResult.worldTransform)
let hitVector = SCNVector3Make(hitTransform.m41,hitTransform.m42,hitTransform.m43)
createBall(position: hitVector)
}
func createBall(position: SCNVector3){
var ballShape = SCNSphere(radius: 0.01)
var ballNode = SCNNode(geometry: ballShape)
ballNode.position = position
sceneView.scene.rootNode.addChildNode(ballNode)
}
// MARK: - ARSCNViewDelegate
func session(_ session: ARSession, didFailWithError error: Error) {
// Present an error message to the user
}
func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}
func sessionInterruptionEnded(_ session: ARSession) {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
}
I inserted this relevant part:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {return}
let result = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
guard let hitResult = result.last else {return}
let hitTransform = SCNMatrix4.init(hitResult.worldTransform)
let hitVector = SCNVector3Make(hitTransform.m41,hitTransform.m42,hitTransform.m43)
createBall(position: hitVector)
}
func createBall(position: SCNVector3){
var ballShape = SCNSphere(radius: 0.01)
var ballNode = SCNNode(geometry: ballShape)
ballNode.position = position
sceneView.scene.rootNode.addChildNode(ballNode)
}
This app runs fine. It places the ball everytime i tap on the screen. Now I wanted to Add a slider to the Main.Storyboard that changes the size value of the ball to be placed. Main.Storyboard looks like this:

When I drag the Slider on the Storyboard Screen, the slider is not added to the scene but it places instead of the ARSCNView to full scale. Then it looks like this:

What do I have to do to just place the slider over the View? Does this Work with other UI Elements? Is this even possible and how does it work. It's probably simple but it's my first time coding in Xcode.
You can't Drag&drop UI controls to ARSCNView. (it shouldn't host subviews) You need to
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With