swift初心者です。UIKitのコードベースでARのアプリ開発を行なっています。
こちらの記事を参考にオブジェクトがタップされた際に画面遷移を行いたいのですが、オブジェクトのタップ自体が検出されず実装できていません。RealityKItにお詳しい方ご指導の方よろしくお願いいたします。
ARViewContoroller
1import UIKit 2import ARKit 3import RealityKit 4import Combine 5 6class ARViewController: UIViewController, ARCoachingOverlayViewDelegate { 7 8 let userView_Button = UIButton() 9 private var arView: ARView! 10 private let coachingOverlay = ARCoachingOverlayView() 11 private var animationEnd: Cancellable! 12 private var go = true 13 private var pin:Entity! 14 private var moveFoward = false 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 arView = ARView(frame: view.bounds) 19 view.addSubview(arView) 20 UISetup() 21 view.backgroundColor = .darkGray 22 navigationItem.title = "AR" 23 24 let tap = UITapGestureRecognizer(target: self, action:#selector(onTap(_:)) ) 25 arView.addGestureRecognizer(tap) 26 } 27 28 @objc func onTap(_ sender: UITapGestureRecognizer? = nil) { 29 guard let touchInView = sender?.location(in: self.arView) else { 30 return 31 } 32 guard self.arView.entity( 33 at: touchInView) != nil else { 34 return 35 } 36 print("pbject tapped") 37 let PIVC = PostInfoViewController() 38 self.present(PIVC, animated: true) 39 } 40 41 @objc func UserViewPressed(_ sender: UIButton) { 42 print("pushed!!!!!") 43 let MenuVC = MenuViewController() 44 let MenuNVC = UINavigationController(rootViewController: MenuVC) 45 if #available(iOS 15.0, *) { 46 if let sheet = MenuNVC.sheetPresentationController { 47 sheet.detents = [.medium(), .large()] 48 sheet.prefersGrabberVisible = true 49 sheet.preferredCornerRadius = 23 50 sheet.largestUndimmedDetentIdentifier = .medium 51 sheet.prefersScrollingExpandsWhenScrolledToEdge = false 52 } 53 } else { 54 // Fallback on earlier versions 55 } 56 self.present(MenuNVC, animated: true) 57 } 58 override func viewWillAppear(_ animated: Bool) { 59 super.viewWillAppear(animated) 60 let config = ARWorldTrackingConfiguration() 61 config.planeDetection = [.horizontal] 62 config.environmentTexturing = .automatic 63 arView.environment.sceneUnderstanding.options = [.occlusion] 64 arView.session.run(config, options: []) 65 coachingOverlay.goal = .horizontalPlane 66 coachingOverlay.activatesAutomatically = false 67 coachingOverlay.session = arView.session 68 coachingOverlay.delegate = self 69 coachingOverlay.frame = arView.bounds 70 arView.addSubview(coachingOverlay) 71 placepins() 72 } 73 74 override func viewWillDisappear(_ animated: Bool) { 75 animationEnd.cancel() 76 super.viewWillDisappear(animated) 77 arView.session.pause() 78 } 79 80 override func viewDidDisappear(_ animated: Bool) { 81 super.viewDidDisappear(animated) 82 83 } 84 85 private func placepins() { 86 guard let pin = try? Entity.load(named: "teapot") else { return } 87 self.pin = pin 88 let anchor = AnchorEntity() 89 anchor.addChild(pin) 90 91 arView.scene.addAnchor(anchor) 92 93 pin.position = [0,0,-60] 94 pin.setScale([100,100,100], relativeTo: pin) 95 96 } 97 98 99}
あなたの回答
tips
プレビュー