ARKit:如何将 UIView 添加到 ARKit 场景? [英] ARKit: How can I add a UIView to ARKit Scene?

查看:45
本文介绍了ARKit:如何将 UIView 添加到 ARKit 场景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ARKit 进行 AR 项目.

I am working on an AR project using ARKit.

我想在 ARKit 场景中添加一个 UIView.当我点击一个对象时,我想以该对象旁边的弹出窗口"形式获取信息.此信息位于 UIView 中.

I want to add a UIView to ARKit Scene. When I tap on an object, I want to get information as a "pop-up" next to the object. This information is in a UIView.

是否可以将此 UIView 添加到 ARKit 场景中?我将此 UIView 设置为场景,然后我该怎么办?我可以给它一个节点,然后将它添加到 ARKit 场景中吗?如果是这样,它是如何工作的?

Is it possible to add this UIView to ARKit Scene? I set up this UIView as a scene and what can I do then? Can I give it a node and then add it to the ARKit Scene? If so, how it works?

或者有其他方法吗?

谢谢!

我的 SecondViewController 的代码

Code of my SecondViewController

class InformationViewController: UIViewController {

    @IBOutlet weak var secondView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view = secondView
    }
}

编辑 2:firstViewController 中的代码

EDIT 2: Code in firstViewController

guard let secondViewController = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else {
    print ("No secondController")
    return
}

let plane = SCNPlane(width: CGFloat(0.1), height: CGFloat(0.1))       
plane.firstMaterial?.diffuse.contents = secondViewController.view

let node = SCNNode(geometry: plane)

我只看到飞机的白屏,而不是视图.

I only get a white screen of a plane, not the view.

推荐答案

实现这一目标的最简单(尽管未公开)方法是将视图控制器支持的 UIView 设置为材质的漫反射内容在 SCNPlane (或任何其他几何图形上,但由于显而易见的原因,它最适合平面).

The simplest (although undocumented) way to achieve that is to set a UIView backed by a view controller as diffuse contents of a material on a SCNPlane (or any other geometry really, but it works best with planes for obvious reasons).

let plane = SCNPlane()
plane.firstMaterial?.diffuse.contents = someViewController.view
let planeNode = SCNNode(geometry: plane)

您必须将视图控制器保留在某处,否则它将被释放并且平面将不可见.只使用 UIView 而没有任何 UIViewController 会抛出错误.

You will have to persist the view controller somewhere otherwise it's going to be released and plane will not be visible. Using just a UIView without any UIViewController will throw an error.

它最好的一点是它保留了所有手势,并且实际上就像一个简单的视图一样工作.例如,如果您使用 UITableViewController 的视图,您将能够在场景中直接滚动它.

The best thing about it is that it keeps all of the gestures and practically works just as a simple view. For example, if you use UITableViewController's view you will be able to scroll it right inside a scene.

我没有在 iOS 10 及更低版本上测试过它,但到目前为止它一直在 iOS 11 上运行.适用于普通 SceneKit 场景和 ARKit.

I haven't tested it on iOS 10 and lower, but it's been working on iOS 11 so far. Works both in plain SceneKit scenes and with ARKit.

这篇关于ARKit:如何将 UIView 添加到 ARKit 场景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆