gpt4 book ai didi

ios - 在 ARKit (Xcode) 中的 ARSCNView 上滑动

转载 作者:搜寻专家 更新时间:2023-10-31 22:04:43 25 4
gpt4 key购买 nike

所以我是 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 添加一个 slider ,以更改要放置的球的大小值。Main.Storyboard 看起来像这样:

enter image description here

当我在 Storyboard屏幕上拖动 slider 时, slider 不会添加到场景中,但它会代替 ARSCNView 放置到全尺寸。然后它看起来像这样:

enter image description here

我需要做什么才能将 slider 放在 View 上?这是否适用于其他 UI 元素?这甚至可能吗?它是如何工作的。这可能很简单,但这是我第一次在 Xcode 中编码。

最佳答案

您不能将 UI 控件拖放到 ARSCNView。 (它不应该承载 subview )你需要

  1. 将 UIView 拖到场景中,
  2. 然后将 ARSCNView 拖到它上面,
  3. 添加约束(例如 top,bottom,left,right = 0),
  4. 将您的 ARSCNView 链接到 sceneView
  5. 添加 slider 。确保 slider 位于 ARSCNView 上方(层次结构较低),否则它将隐藏在后面。

关于ios - 在 ARKit (Xcode) 中的 ARSCNView 上滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48192617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com