티스토리 뷰

반응형

 

PageControl

 

/// 커스텀 페이지 컨트롤 애니메이션 Custom PageControl + Animation
struct PageControl: View {
    
    var numberOfPages: Int
    @Binding var currentPage: Int
    
    var body: some View {
        HStack(spacing: 8) {
            ForEach(0..<numberOfPages, id: \.self) { pagingIndex in
                
                let isCurrentPage = currentPage == pagingIndex
                let height = 8.0
                let width = isCurrentPage ? height * 2 : height
                
                Capsule()
                    .fill(isCurrentPage ? .blue : .gray.opacity(0.5))
                    .frame(width: width, height: height)
            }
        }
        .animation(.linear, value: currentPage)
    }
}

#Preview {
    PageControl(numberOfPages: 3, currentPage: .constant(0))
}

 

 

struct ContentView: View {
    
    let array: [Color] = [.red, .green, .blue]
    
    @State var selection = 0
    
    var body: some View {
        
        VStack {
            TabView(selection: $selection) {
                ForEach(array.indices, id: \.self) {
                    array[$0]
                        .tag($0)
                }
            }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .frame(width: 300, height: 300)
            
            PageControl(numberOfPages: array.count, currentPage: $selection)
        }
    }
}

iOS

Swift

Xcode

PageControl

UIPageControl

TabView

tabViewStyle

indexDisplayMode

페이지컨트롤

인디케이터

indicator

반응형
댓글
300x250
반응형
최근에 올라온 글
최근에 달린 댓글
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Total
Today
Yesterday