forked from agarwalayush9/Shelves
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnboardingView.swift
176 lines (155 loc) · 6.42 KB
/
OnboardingView.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// OnboardingView.swift
// Shelves-User
//
// Created by Suraj Singh on 04/07/24.
//
import SwiftUI
extension Color {
var components: (red: CGFloat, green: CGFloat, blue: CGFloat, opacity: CGFloat) {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var opacity: CGFloat = 0
UIColor(self).getRed(&red, green: &green, blue: &blue, alpha: &opacity)
return (red, green, blue, opacity)
}
}
func interpolateColor(from startColor: Color, to endColor: Color, at position: CGFloat) -> Color {
let t = min(max(position, 0), 1)
let startComponents = startColor.components
let endComponents = endColor.components
let interpolatedColor = Color(
red: (1 - t) * startComponents.red + t * endComponents.red,
green: (1 - t) * startComponents.green + t * endComponents.green,
blue: (1 - t) * startComponents.blue + t * endComponents.blue
)
return interpolatedColor
}
struct OnboardingView: View {
@Binding var showOnboarding: Bool
@State private var currentIndex = 0
private let totalSteps = 3
private let animationDuration: Double = 0.1 // Increase duration for slower animation
// Define the gradient colors
private let startColor = Color(red: 1.0, green: 0.9, blue: 0.7)
private let endColor = Color(red: 1.0, green: 0.8, blue: 0.5)
var body: some View {
ZStack {
LinearGradient(
gradient: Gradient(colors: [startColor, endColor]),
startPoint: .top,
endPoint: .bottom
)
.edgesIgnoringSafeArea(.all)
VStack {
TabView(selection: $currentIndex) {
OnboardingStepView(
image: "bookshelf",
title: "Explore Unlimited Books",
description: "Find your next favorite read"
)
.tag(0)
OnboardingStepView(
image: "books", // Replace with your image name
title: "One-Click Borrowing",
description: "Instantly get your favorite books"
)
.tag(1)
OnboardingStepView(
image: "reading", // Replace with your image name
title: "Reserve Your Reads",
description: "Save books for later pickup"
)
.tag(2)
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never)) // Disable default page indicator
// Page Control
PageControl(numberOfPages: totalSteps, currentPage: $currentIndex)
.padding(.bottom, 20)
Spacer()
HStack {
if currentIndex == 1 {
Button(action: {
showOnboarding = false
}) {
Text("Skip")
.foregroundColor(Color(red: 81/255, green: 58/255, blue: 16/255)).bold()
.padding()
.frame(width: 120, height: 65)
.background(
LinearGradient(
gradient: Gradient(colors: [startColor, endColor]),
startPoint: .top,
endPoint: .bottom
)
)
.cornerRadius(10)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color(red: 0.4, green: 0.2, blue: 0.1), lineWidth: 2)
)
}
Spacer()
.frame(width: 16) // Adjust this width value to reduce/increase padding
Button(action: {
withAnimation(.easeInOut(duration: animationDuration)) {
currentIndex += 1
}
}) {
Text("Next")
.foregroundColor(.white)
.padding().bold()
.frame(width: 180, height: 65)
.background(Color(red: 81/255, green: 58/255, blue: 16/255))
.cornerRadius(10)
}
} else {
Spacer()
Button(action: {
withAnimation(.easeInOut(duration: animationDuration)) {
if currentIndex < totalSteps - 1 {
currentIndex += 1
} else {
showOnboarding = false
}
}
}) {
Text(currentIndex < totalSteps - 1 ? "Next" : "Finish")
.foregroundColor(.white)
.padding()
.frame(width: 205, height: 65)
.background(Color(red: 81/255, green: 58/255, blue: 16/255))
.cornerRadius(10)
}
Spacer()
}
}
.padding()
}
}
}
}
struct OnboardingStepView: View {
var image: String
var title: String
var description: String
var body: some View {
VStack(spacing: 20) {
Spacer()
Image(image)
.resizable()
.scaledToFit()
.frame(height: 200)
Text(title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color(red: 81/255, green: 58/255, blue: 16/255))
Text(description)
.font(.subheadline)
.foregroundColor(Color(red: 81/255, green: 58/255, blue: 16/255))
Spacer()
}
.padding()
}
}