我对 SwiftUI 的新 MapKit 功能很感兴趣,而且我喜欢
LookAroundPreview
的创建是多么简单。但文档声称有一个
.lookAroundViewer(isPresented: $isLookingAround, initialScene: lookAroundScene)
可以应用于现有视图的方法,并且......好吧,它不存在 - 至少编译器不这么认为。需要明确的是,文档说:
要显示用户可以探索的“环视”查看器,请将lookAroundViewer 视图修饰符应用于特定视图,然后添加用户与之交互的控件以显示“环视”查看器。在下面的示例中,lookAroundViewer 视图修饰符观察到布尔值的绑定,以确定是否显示 Look around 查看器。
现在,我有一个静态的
LookAroundPreview
工作(这真是太棒了,只需要这么一点点努力),但我认为文档有点超前了......
我的问题是“
.lookAroundViewer(isPresented: $isLookingAround, initialScene: lookAroundScene)
实际上已经在Xcode 15b3中实现了,如果是这样,我该如何调用它?”。
非常感谢。这是工作代码...
@State private var lookAroundScene: MKLookAroundScene?
@State private var isLookingAround = false
var body: some View {
VStack {
item.image
.resizable()
.aspectRatio(contentMode: .fill)
//.lookAroundViewer(isPresented: $isLookingAround, initialScene: lookAroundScene) //<--- DOESN'T WORK
if lookAroundScene.exists {
Button(systemImage: "binoculars") { isLookingAround = true }
}
}
.onAppear(perform: getLookAroundScene)
.popover(isPresented: $isLookingAround) {
LookAroundPreview(initialScene: lookAroundScene)
}
}
private func getLookAroundScene() {
lookAroundScene = nil
Task {
let request = MKLookAroundSceneRequest(coordinate: item.coordinate)
lookAroundScene = try? await request.scene
}
}
这似乎现在有效。
.lookAroundViewer
行取消注释时,触摸后视图将变为全屏。如果您评论第二个LookaroundPreview
,第一个似乎错过了重要的初始化,并且全屏预览出现但为空。完整代码:https://gist.github.com/thadk/ea2e067b246b3bf7d96b94115da1f846