ARKit:如何在ARKit场景中添加UIView? [英] ARKit: How can I add a UIView to ARKit Scene?
问题描述
我正在使用ARKit处理AR项目。
I am working on a AR project using ARKit.
我想在ARKit场景中添加一个UIView。当我点击一个对象时,我希望将信息作为对象旁边的弹出窗口。这些信息在UIView中。
I want to add a UIView to ARKit Scene. When I tap on a 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 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:如何在ARKit场景中添加UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!