-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftUIView.swift
60 lines (53 loc) · 1.54 KB
/
SwiftUIView.swift
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//
// SwiftUIView.swift
//
//
// Created by Aman Gupta on 18/01/24.
//
import SwiftUI
struct CarouselSliderView: View {
let titles = ["Title 1", "Title 2", "Title 3", "Title 4", "Title 5"]
@State private var currentIndex: Int = 0
var body: some View {
VStack {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 20) {
ForEach(0..<titles.count, id: \.self) { index in
CarouselCardView(title: titles[index], index: index, currentIndex: $currentIndex)
}
}
.padding()
}
.animation(.easeInOut, value: currentIndex)
Text("Selected Title: \(titles[currentIndex])")
.padding()
}
}
}
struct CarouselCardView: View {
let title: String
let index: Int
@Binding var currentIndex: Int
var body: some View {
VStack {
RoundedRectangle(cornerRadius: 15)
.fill(Color.blue)
.frame(width: 300, height: 200)
.overlay(
Text(title)
.foregroundColor(.white)
.font(.headline)
)
.onTapGesture {
withAnimation {
currentIndex = index
}
}
}
}
}
struct CarouselSliderView_Previews: PreviewProvider {
static var previews: some View {
CarouselSliderView()
}
}