본문 바로가기
컴퓨터언어/Swift

[Swift] Carousel View

by 인턴개구리 2023. 8. 9.

Carousel View는 여러 페이지 또는 아이템을 가로로 슬라이드하면서 보여주는 UI 컴포넌트다. 이런 형식인 듯 하다.



카드형식으로 되어 있어 좌우로 스크롤하는 형식이다.

Chat GPT에 물어봤더니 다음과 같은 코드를 반환했다.

import SwiftUI

struct ContentView: View {
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 0) {
                ForEach(0..<3) { index in
                    Text("Page \(index + 1)")
                        .frame(width: UIScreen.main.bounds.width, height: 200)
                        .background(Color.blue)
                        .foregroundColor(.white)
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


하지만 이 코드에는 자석처럼 고정되는 기능은 없던데, 이것도 캐러셀 뷰라 부르는지, 공부를 더 해야겠다.