forked from fermoya/SwiftUIPager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorsExampleView.swift
120 lines (108 loc) · 4.42 KB
/
ColorsExampleView.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
//
// ColorsExampleView.swift
// SwiftUIPagerExample
//
// Created by Fernando Moya de Rivas on 02/03/2020.
// Copyright © 2020 Fernando Moya de Rivas. All rights reserved.
//
import SwiftUI
struct ColorsExampleView: View {
@StateObject var page: Page = .first()
var colors: [Color] = [
.red, .blue, .black, .gray, .purple, .green, .orange, .pink, .yellow, .white
]
var body: some View {
NavigationView {
GeometryReader { proxy in
VStack(spacing: 10) {
Pager(page: self.page,
data: self.colors,
id: \.self) {
self.pageView($0)
}
.contentLoadingPolicy(.eager)
.disableDragging()
.itemSpacing(10)
.padding(20)
.frame(width: min(proxy.size.height, proxy.size.width),
height: min(proxy.size.height, proxy.size.width))
.background(Color.gray.opacity(0.3))
.navigationBarTitle("Color Picker", displayMode: .inline)
Spacer()
HStack {
Spacer()
Circle()
.fill(self.colors[self.page.index])
.frame(width: 80)
.overlay(Circle().stroke(self.page.index < 4 ? Color.gray.opacity(0.5) : Color.black, lineWidth: 5))
Spacer()
Text("\(self.colors[self.page.index].rgb)")
Spacer()
}
Spacer()
HStack {
Spacer()
Button(action: {
withAnimation {
self.page.update(.moveToFirst)
}
}, label: {
VStack(spacing: 4) {
Image(systemName: "backward.end.alt.fill")
.padding()
Text("Start")
.font(.subheadline)
}
}).disabled(self.page.index <= 0)
Button(action: {
withAnimation {
self.page.update(.previous)
}
}, label: {
VStack(spacing: 4) {
Image(systemName: "backward.end.fill")
.padding()
Text("Previous")
.font(.subheadline)
}
}).disabled(self.page.index <= 0)
Spacer()
Button(action: {
withAnimation {
self.page.update(.next)
}
}, label: {
VStack(spacing: 4) {
Image(systemName: "forward.end.fill")
.padding()
Text("Next")
.font(.subheadline)
}
}).disabled(self.page.index >= self.colors.count - 1)
Button(action: {
withAnimation {
self.page.update(.moveToLast)
}
}, label: {
VStack(spacing: 4) {
Image(systemName: "forward.end.alt.fill")
.padding()
Text("End")
.font(.subheadline)
}
}).disabled(self.page.index >= self.colors.count - 1)
Spacer()
}
Spacer()
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
func pageView(_ color: Color) -> some View {
Rectangle()
.fill(color)
.cornerRadius(5)
.shadow(radius: 5)
}
}