简体   繁体   中英

How to create a horizontal list with only one element shown at a time?

The best way to explain what I'm trying to achieve is to show the picture bellow. 在此处输入图像描述

I would like to implement the horizontal sliding card list with animation as seen above. However I'm new to swiftUI and don't know where to even start so any help would be appreciated. The list will only have 4 elements which will contain a list within them instead of the content. Similar to the image bellow. 在此处输入图像描述

I think what you want is a horizontal ScrollView. Here is a basic implementation.

For more see Hacking with Swift

struct ContentView: View {
    
    let testData : [TestData] = [TestData(title: "Begginer"), TestData(title: "Medium"), TestData(title: "Hard")]
    
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 20) {
                ForEach(testData, id: \.self) { data in
                    Text("\(data.title)")
                        .foregroundColor(.white)
                        .font(.largeTitle)
                        .frame(width: 300, height: 600)
                        .background(Color(.darkGray))
                }
            }
        }
    }
}

struct TestData: Hashable {
    let title: String
}

基本水平滚动视图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM