VisionOS 窗口 3D 位置和旋转

问题描述 投票:0回答:1

我正在模拟器上使用视觉操作系统,并尝试制作一个类似于标准键盘的虚拟键盘(但具有不同的按钮)。我可以在我的应用程序中创建另一个 WindowGroup 并用按钮填充它,并在应用程序需要输入时打开它。

但是,我希望此键盘窗口出现在与标准键盘类似的位置(在主窗口下方),并以类似的 3D 旋转(就好像“躺在桌子上”,但不基于某些物理对象)。如何指定新打开窗口的位置及其 3d 旋转?

我预计会有 WindowGroup 修饰符,但我找不到。

swiftui window visionos
1个回答
0
投票

从 Xcode 16 开始,支持在自定义位置创建新窗口

A keyboard window is positioned directly below the main window.

    import SwiftUI
    
    @main
    struct YourAppName: App {
        @State
        private var mainWindow:WindowProxy?
        var body: some Scene {
            WindowGroup(id: "mainWindow") {
                ContentView()
            }
            
            WindowGroup(id: "KeyboardWindow") {
                KeyboardWindow()
            }
            .defaultSize(.init(width: 200, height: 100))
            .defaultWindowPlacement { content, context in
                if let mainWindow = context.windows.first(where: { window in
                    return (window.id == "mainWindow")
                }) {
                    return WindowPlacement(.below(mainWindow))
                } else {
                    print("No window with ID 'mainWindow' found!")
                    return WindowPlacement()//use default placement if main window not exsist
                }
                
            }
       
         }
    }
    
    
    struct ContentView: View {
        @Environment(\.openWindow)
        var openWindowAction
        var body: some View {
            VStack {
    
                Text("Hello, world!")
    
                Button {
                    openWindowAction(id: "KeyboardWindow")
                } label: {
                    Label("Open Keyboard", systemImage: "keyboard")
                }
                .buttonStyle(.bordered)
    
            }
            .padding()
        }
    }
    
    struct KeyboardWindow: View {
        var body: some View {
            Text("Here is keyboard window")
                .padding(50)
        }
    }
    
    
    #Preview(windowStyle: .automatic) {
        Text("Preview does not support showing multiple window positioning.")
    }
© www.soinside.com 2019 - 2024. All rights reserved.