如何使用TabView或ScrollView进行轮播?

Tru*_*an1 10 swiftui swiftui-scrollview swiftui-tabview

我正在尝试创建一个像苹果的一些应用程序一样的轮播。具有TabView分页样式,但它不会“查看”上一个和下一个元素。不会ScrollView(.horizontal)折断。我希望将这两种行为结合起来,而无需重写客户视图。这两种观点中的任何一种都可能发生这样的事情吗:

在此输入图像描述

这就是我正在尝试的:

ScrollView(.horizontal, showsIndicators: false) {
    HStack(alignment: .center, spacing: 0) {
        GroupBox(label: Text("This is the title 1")) {
            Label("Some item 1", systemImage: "checkmark.circle.fill")
            Label("Some item 2", systemImage: "checkmark.circle.fill")
            Label("Some item 3", systemImage: "checkmark.circle.fill")
            Label("Some item 4", systemImage: "checkmark.circle.fill")
        }
        .padding()
        GroupBox(label: Text("This is the title 2")) {
            Label("Some item 1", systemImage: "checkmark.circle.fill")
            Label("Some item 2", systemImage: "checkmark.circle.fill")
            Label("Some item 3", systemImage: "checkmark.circle.fill")
            Label("Some item 4", systemImage: "checkmark.circle.fill")
        }
        .padding()
        GroupBox(label: Text("This is the title 3")) {
            Label("Some item 1", systemImage: "checkmark.circle.fill")
            Label("Some item 2", systemImage: "checkmark.circle.fill")
            Label("Some item 3", systemImage: "checkmark.circle.fill")
            Label("Some item 4", systemImage: "checkmark.circle.fill")
        }
        .padding()
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

TabView {
    GroupBox(label: Text("This is the title 1")) {
        Label("Some item 1", systemImage: "checkmark.circle.fill")
        Label("Some item 2", systemImage: "checkmark.circle.fill")
        Label("Some item 3", systemImage: "checkmark.circle.fill")
        Label("Some item 4", systemImage: "checkmark.circle.fill")
    }
    .padding()
    GroupBox(label: Text("This is the title 2")) {
        Label("Some item 1", systemImage: "checkmark.circle.fill")
        Label("Some item 2", systemImage: "checkmark.circle.fill")
        Label("Some item 3", systemImage: "checkmark.circle.fill")
        Label("Some item 4", systemImage: "checkmark.circle.fill")
    }
    .padding()
    GroupBox(label: Text("This is the title 3")) {
        Label("Some item 1", systemImage: "checkmark.circle.fill")
        Label("Some item 2", systemImage: "checkmark.circle.fill")
        Label("Some item 3", systemImage: "checkmark.circle.fill")
        Label("Some item 4", systemImage: "checkmark.circle.fill")
    }
    .padding()
}
.tabViewStyle(PageTabViewStyle())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述