ios - 如何在我的 swiftui 应用程序中嵌入苹果的演示:Capturing Body Motion in 3D?
问题描述
这是我第一次使用swiftui,所以我不太清楚如何在我的swiftui App中使用演示。我发现demo是基于UIKIT的,但是我的App是基于swiftui的,所以不知道demo如何适配App。我试图将 HumanViewController 包装如下:
struct HumanViewControllerControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = HumanViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<HumanViewControllerControllerWrapper>) -> HumanViewControllerControllerWrapper.UIViewControllerType {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController: HumanViewController = mainStoryboard.instantiateViewController(withIdentifier: "Main1") as! HumanViewController
return mainViewController
}
func updateUIViewController(_ uiViewController: HumanViewControllerControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<HumanViewControllerControllerWrapper>) {
//
}
}
我所有的代码:
//
// ContentView.swift
// random name
//
//
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink("click here", destination: HumanViewControllerControllerWrapper())
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct HumanViewControllerControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = HumanViewController
func makeUIViewController(context: UIViewControllerRepresentableContext<HumanViewControllerControllerWrapper>) -> HumanViewControllerControllerWrapper.UIViewControllerType {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController: HumanViewController = mainStoryboard.instantiateViewController(withIdentifier: "Main1") as! HumanViewController
return mainViewController
}
func updateUIViewController(_ uiViewController: HumanViewControllerControllerWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<HumanViewControllerControllerWrapper>) {
//
}
}
//
// HumanViewController.swift
// random name
//
//
import Foundation
/*
See LICENSE folder for this sample’s licensing information.
Abstract:
The sample app's main view controller.
*/
import UIKit
import RealityKit
import ARKit
import Combine
public class HumanViewController: UIViewController, ARSessionDelegate {
@IBOutlet var arView: ARView!
// The 3D character to display.
var character: BodyTrackedEntity?
let characterOffset: SIMD3<Float> = [-1.0, 0, 0] // Offset the character by one meter to the left
let characterAnchor = AnchorEntity()
public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// If the iOS device doesn't support body tracking, raise a developer error for
// this unhandled case.
guard ARBodyTrackingConfiguration.isSupported else {
fatalError("This feature is only supported on devices with an A12 chip")
}
// Run a body tracking configration.
let configuration = ARBodyTrackingConfiguration()
arView.session.delegate = self
arView.session.run(configuration)
arView.scene.addAnchor(characterAnchor)
// Asynchronously load the 3D character.
var cancellable: AnyCancellable? = nil
cancellable = Entity.loadBodyTrackedAsync(named: "character/robot").sink(
receiveCompletion: { completion in
if case let .failure(error) = completion {
print("Error: Unable to load model: \(error.localizedDescription)")
}
cancellable?.cancel()
}, receiveValue: { (character: Entity) in
if let character = character as? BodyTrackedEntity {
// Scale the character to human size
character.scale = [1.0, 1.0, 1.0]
self.character = character
cancellable?.cancel()
} else {
print("Error: Unable to load model as BodyTrackedEntity")
}
})
}
public func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
for anchor in anchors {
guard let bodyAnchor = anchor as? ARBodyAnchor else { continue }
// Update the position of the character anchor's position.
let bodyPosition = simd_make_float3(bodyAnchor.transform.columns.3)
characterAnchor.position = bodyPosition + characterOffset
// Also copy over the rotation of the body anchor, because the skeleton's pose
// in the world is relative to the body anchor's rotation.
characterAnchor.orientation = Transform(matrix: bodyAnchor.transform).rotation
if let character = character, character.parent == nil {
// Attach the character to its anchor as soon as
// 1. the body anchor was detected and
// 2. the character was loaded.
characterAnchor.addChild(character)
}
}
}
}
//
// random_nameApp.swift
// random name
//
//
import SwiftUI
@main
struct random_nameApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
//add code when didFinishLaunchingWithOptions
return true
}
}
当我单击设备上的链接时它失败了。日志是:
Unexpectedly found nil while implicitly unwrapping an Optional value: file random_name/HumanViewController.swift, line 42
那么我该如何处理这个错误或者任何人都可以告诉我如何在我的应用程序中嵌入演示?先感谢您。
解决方案
推荐阅读
- android - Android接口未初始化 - android中的回调
- excel - 如何在 Excel 中的多个选项卡中将特定单元格颜色更改为无填充?
- python - 关于 QTreeView 中的项目和信号的信息 - PyQt5
- python - 更改数据存储多路径策略
- c# - 无法反序列化 Jason 文件虽然得到了响应
- c# - 如何使 dll 中的代码适应主代码?
- vb.net - 如何使用带方括号的 SelectToken?
- python - Python如何从excel中迭代特定行
- c# - 最小化/最大化表单会导致在焦点中设置多个组合框。这里发生了什么?
- javascript - 从节点和 Typescript 应用程序连接 Vertica DB