【发布时间】:2018-01-11 05:14:14
【问题描述】:
所以我是 Swift、Xcode 和 Arkit 的新手。 我使用 Xcode 9.2
我想构建一个简单的应用程序,当在屏幕上点击一个对象时,它会放置到它检测到一个平面或点的位置。
我启动了 Xcode,并将这段简短的代码写入了基本示例中,当您打开 scenekit 时就在该示例中。 代码如下所示:
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
}
}
我插入了这个相关部分:
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)
}
这个应用程序运行良好。每次我点击屏幕时,它都会放置球。 现在我想向 Main.Storyboard 添加一个滑块,以更改要放置的球的大小值。 Main.Storyboard 如下所示:
当我在情节提要屏幕上拖动滑块时,滑块不会添加到场景中,而是将 ARSCNView 放置到全尺寸。然后它看起来像这样:
我需要做什么才能将滑块放在视图上?这是否适用于其他 UI 元素?这甚至可能吗?它是如何工作的。这可能很简单,但这是我第一次在 Xcode 中编码。
【问题讨论】:
-
如果您不想要陡峭的学习曲线……我建议您在操场上使用小型编码 sn-ps 来学习基础知识。在掌握了各个框架的基础知识之后,你会更容易理解 xcode 的界面,尤其是 scenekit 编辑器。我在下面添加了一个游乐场,如果您想采用这种方法,它将帮助您入门。