【问题标题】:Adding a Welcome ViewController screen in front of ViewController ARSession在 ViewController ARSession 前面添加一个 Welcome ViewController 屏幕
【发布时间】:2020-10-09 00:53:04
【问题描述】:

我正在开发一个应用程序,我想添加“欢迎!”屏幕上有一个按钮,可以将用户带到 AR 会话。像往常一样,AR 会话在第一个 View Controller 上设置。添加提示用户进入 AR 会话的屏幕(不是启动屏幕)的最佳方式是什么? 我尝试创建一个新的 ViewController 并在现有代码之前包含代码,但这对我不起作用。我从来没有真正改变过这样的屏幕,所以非常感谢任何建议!

import UIKit
import RealityKit
import ARKit
    

 class ViewController: UIViewController, ARSessionDelegate {
     //delay app launch to show splash screen
    func application(_ application: UIApplication, didFinishLaunchingWithOptions 
  launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Thread.sleep(forTimeInterval: 3.0)
        // Override point for customization after application launch.
        return true
    }
  //end splash screen delay
  @IBOutlet var arView: ARView!

  override func viewDidLoad() {
    super.viewDidLoad()
 
    arView.session.delegate = self
    
    showModel()
    overlayCoachingView()
    setupARView()
    
    arView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(recognizer:))))
    
}

func showModel(){
    
    let anchorEntity = AnchorEntity(plane: .horizontal, minimumBounds:[0.7, 0.7])
    
   anchorEntity.scale = [0.2, 0.2, 0.2]
    
    let entity = try! Entity.loadModel(named: "COW_ANIMATIONS")
    entity.setParent(anchorEntity)
    
    arView.scene.addAnchor(anchorEntity)
    

}
//Overlay coaching view "adjust iphone scan"
func overlayCoachingView () {
    
    let coachingView = ARCoachingOverlayView(frame: CGRect(x: 0, y: 0, width: 
 arView.frame.width, height: arView.frame.height))
    
    coachingView.session = arView.session
    coachingView.activatesAutomatically = true
    coachingView.goal = .horizontalPlane
    
    view.addSubview(coachingView)
    
}//end overlay
    

func setupARView(){
    arView.automaticallyConfigureSession = false
    let configuration = ARWorldTrackingConfiguration()
    configuration.planeDetection = [.horizontal, .vertical]
    configuration.environmentTexturing = .automatic
    arView.session.run(configuration)
}

//object placement

@objc
func handleTap(recognizer: UITapGestureRecognizer){
    let location = recognizer.location(in:arView)
    
    let results = arView.raycast(from: location, allowing: .estimatedPlane, alignment: .horizontal)
    
    if let firstResult = results.first {
        let brownCowAnchor = ARAnchor(name: "COW_ANIMATIONS", transform: firstResult.worldTransform)
        arView.session.add(anchor: brownCowAnchor)
    } else {
        print("Object placement failed - couldn't find surface.")
        
        
        
        //cow animations
        //let robot = try! ModelEntity.load(named: "COW_ANIMATIONS")
       let brownCowAnchor = AnchorEntity()
        let blackCowAnchor = AnchorEntity()
  
        //anchor.children.append(robot)
        //arView.scene.anchors.append(anchor)

        //robot.playAnimation(robot.availableAnimations[0].repeat(duration: .infinity),
                                                      //transitionDuration: 0.5,
                                                            //startsPaused: false)
        
        //start cow animation
        
        
        let brownCow = try! ModelEntity.load(named: "COW_ANIMATIONS")
        let blackCow = try! ModelEntity.load(named: "Cow")

        brownCow.position.x = -1.0
        blackCow.position.x = 1.0
        brownCowAnchor.position.z = -2.0
        blackCowAnchor.position.z = -2.0
        brownCow.setParent(brownCowAnchor)
        blackCow.setParent(blackCowAnchor)
        arView.scene.anchors.append(brownCowAnchor)
        arView.scene.anchors.append(blackCowAnchor)
        
        let cowAnimationResource = brownCow.availableAnimations[0]
        let horseAnimationResource = blackCow.availableAnimations[0]

        brownCow.playAnimation(cowAnimationResource.repeat(duration: .infinity),
                                            transitionDuration: 1.25,
                                                  startsPaused: false)

        blackCow.playAnimation(horseAnimationResource.repeat(duration: .infinity),
                                                transitionDuration: 0.75,
                                                      startsPaused: false)
        
        //end cow animations
    }
  }
  
  func placeObject(named entityName: String, for anchor: ARAnchor)  {
    let entity = try! ModelEntity.loadModel(named: entityName)
    
    entity.generateCollisionShapes(recursive: true)
    arView.installGestures([.rotation, .translation], for: entity)
    
    
    let anchorEntity = AnchorEntity(anchor: anchor)
    anchorEntity.addChild(entity)
    arView.scene.addAnchor(anchorEntity)
    
    
   }
 }

【问题讨论】:

  • 不要睡在主线程上!!!
  • 对不起,我有点困惑。什么意思?
  • 你在做Thread.sleep(forTimeInterval: 3.0)。为什么!?
  • 哦,对不起!是的,所以这个应用程序只适合一个人作为圣诞礼物。我正在为启动画面制作一个内部笑话徽标,所以我想将它延迟一秒钟,因为它通常不会出现。它不会有拥挤的交通,并且可能只有一个人使用
  • 不错!我建议改用DispatchQueue.main.asyncAfter(deadline: .now() + 3.0, execute: { self.hideSplashScreen() })。

标签: swift xcode viewcontroller arkit realitykit


【解决方案1】:

首先,您可以拖动情节提要中的箭头来设置您的初始视图控制器。

然后,为欢迎屏幕创建一个新的UIViewController 类。

class WelcomeViewController: UIViewController {


}

然后,在故事板中,将欢迎视图控制器设置为 WelcomeViewController 类。

通过@IBAction 将“Let's go”按钮连接到WelcomeViewController。

在@IBAction 中,执行以下操作:

@IBAction func goPressed(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController {
        self.present(viewController, animated: true, completion: nil) /// present the view controller (the one with the ARKit)!
    }
}

然后,回到情节提要,将Storyboard ID 设置为主视图控制器(带有ARSCNView 的那个)为ViewController:

就是这样!

【讨论】:

  • 非常感谢!!这是一个非常清晰和有用的描述!现在我遇到的唯一问题是线程 1:“-[_0_2_2020_2.WelcomeViewControllerletsGo:]:无法识别的选择器发送到实例 0x102a0b430”错误显示在我的应用程序崩溃后。我在这里提出了新问题 - stackoverflow.com/questions/64282803/…
  • Np。检查您的@IBActions。此外,您可能希望将 WelcomeViewController 的代码放在单独的文件中。
猜你喜欢
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多