This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageIndicator.swift
89 lines (77 loc) · 2.27 KB
/
PageIndicator.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
//
// PageIndicator.swift
//
// Created by Oleksandr Glagoliev on 6/24/20.
// Copyright © 2020 Oleksandr Glagoliev. All rights reserved.
//
import SwiftUI
// MARK: - Dot Indicator -
struct DotIndicator: View {
let minScale: CGFloat = 1
let maxScale: CGFloat = 1.1
let minOpacity: Double = 0.6
let pageIndex: Int
@Binding var slectedPage: Int
var body: some View {
Button(action: {
self.slectedPage = self.pageIndex
}) {
Circle()
.scaleEffect(
slectedPage == pageIndex
? maxScale
: minScale
)
.animation(.spring())
.foregroundColor(
slectedPage == pageIndex
? Color.black
: Color.black.opacity(minOpacity)
)
}
}
}
// MARK: - Page Indicator -
struct PageIndicator: View {
// Constants
private let spacing: CGFloat = 2
private let diameter: CGFloat = 8
// Settings
let numPages: Int
@Binding var selectedIndex: Int
init(numPages: Int, currentPage: Binding<Int>) {
self.numPages = numPages
self._selectedIndex = currentPage
}
var body: some View {
VStack {
HStack(alignment: .center, spacing: spacing) {
ForEach((0..<numPages)) {
DotIndicator(
pageIndex: $0,
slectedPage: self.$selectedIndex
).frame(
width: self.diameter,
height: self.diameter
)
}
}
}
}
}
// MARK: - Previews -
struct DotIndicator_Previews: PreviewProvider {
static var previews: some View {
DotIndicator(pageIndex: 0, slectedPage: .constant(0))
.previewLayout(.fixed(width: 200, height: 200))
.previewDisplayName("Hello")
}
}
struct PageIndicator_Previews: PreviewProvider {
static var previews: some View {
PageIndicator(numPages: 5, currentPage: .constant(2))
.previewDisplayName("Regular")
.previewLayout(PreviewLayout.sizeThatFits)
.padding()
}
}