有Arview installestures问题
我正在Swiftui中使用uiviewRementable
创建一个Arview,并且我正在尝试将所有EntityGestures
应用于模型,但是我不确定为什么手势不起作用,并且<<代码> arview 没有接收任何手势。这是代码:
func makeUIView(context: Context) -> ARView {
let view = ARView()
.
.
.
.
// Handle ARSession events via delegate
context.coordinator.view = view
session.delegate = context.coordinator
view.addGestureRecognizer(UITapGestureRecognizer(target: context.coordinator, action: #selector(ARCoordinator.handleTap)))
return view
}
func updateUIView(_ view: ARView, context: Context) { }
func makeCoordinator() -> ARCoordinator {
ARCoordinator()
}
}
class ARCoordinator: NSObject, ARSessionDelegate {
weak var view: ARView?
var focusEntity: FocusEntity?
private var isModelPlaced = false
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
guard let view = self.view else { return }
debugPrint("Anchors added to the scene: ", anchors)
self.focusEntity = FocusEntity(on: view, style: .classic(color: .yellow))
}
@objc func handleTap() {
if isModelPlaced == false {
guard let view = self.view, let focusEntity = self.focusEntity else { return }
// Create a new anchor to add content to
let anchor = AnchorEntity()
view.scene.anchors.append(anchor)
// Add a model
let modelEntity = try! ModelEntity.loadModel(named: "Models.scnassets/ball")
modelEntity.generateCollisionShapes(recursive: true)
modelEntity.position = focusEntity.position
view.installGestures(.all, for: modelEntity) //*** gestures is not working ***///
focusEntity.hide()
isModelPlaced = true
anchor.addChild(modelEntity)
}
}
}
任何帮助都会很棒
I am creating an ARView using UIViewRepresentable
in SwiftUI, and I am trying to apply all EntityGestures
to the model, but I am not sure why the gestures are not working and the ARView
is not receiving any gestures. Here is the code:
func makeUIView(context: Context) -> ARView {
let view = ARView()
.
.
.
.
// Handle ARSession events via delegate
context.coordinator.view = view
session.delegate = context.coordinator
view.addGestureRecognizer(UITapGestureRecognizer(target: context.coordinator, action: #selector(ARCoordinator.handleTap)))
return view
}
func updateUIView(_ view: ARView, context: Context) { }
func makeCoordinator() -> ARCoordinator {
ARCoordinator()
}
}
class ARCoordinator: NSObject, ARSessionDelegate {
weak var view: ARView?
var focusEntity: FocusEntity?
private var isModelPlaced = false
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
guard let view = self.view else { return }
debugPrint("Anchors added to the scene: ", anchors)
self.focusEntity = FocusEntity(on: view, style: .classic(color: .yellow))
}
@objc func handleTap() {
if isModelPlaced == false {
guard let view = self.view, let focusEntity = self.focusEntity else { return }
// Create a new anchor to add content to
let anchor = AnchorEntity()
view.scene.anchors.append(anchor)
// Add a model
let modelEntity = try! ModelEntity.loadModel(named: "Models.scnassets/ball")
modelEntity.generateCollisionShapes(recursive: true)
modelEntity.position = focusEntity.position
view.installGestures(.all, for: modelEntity) //*** gestures is not working ***///
focusEntity.hide()
isModelPlaced = true
anchor.addChild(modelEntity)
}
}
}
any help would be great
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,在导入的3D型号中添加手势与简单生成的形状相同。如果您具有带有生成箱网格或生成球的模型态,您的代码将正常工作。但是,我不确定为什么进口模型的过程有所不同。
这是一个可能有帮助的另一个问题的链接:启用extrable erally intreakit in retacekit
苹果开发人员论坛上的某人有同样的问题。如果您在上面的链接问题中找不到想要的内容,这是您的下一个最好的选择: https://developer.apple.com/forums/thread/119773
我纠正您的代码来自 on RealityKit?如果这两个链接都无法正常工作,您可能会直接在Q&amp; a中直接问他。
As far as I'm aware, adding gestures to imported 3D models does not work the same as for simple generated shapes. Your code would work fine if you had a ModelEntity with a mesh of GenerateBox or GenerateSphere. I'm not sure why the process is different for imported models, though.
Here's a link to another question that might help: Enabling gestures in RealityKit
Someone on the apple developer forum had the same problem. If you can't find what you're looking for in the linked question above, this is your next best bet: https://developer.apple.com/forums/thread/119773
Am I correct that your code is from Mohammad Azam's Udemy course on RealityKit? If neither of those links work, you might have luck asking him directly in the Q&A.