-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegmentedControl.swift
171 lines (144 loc) · 5.07 KB
/
SegmentedControl.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
//
// SegmentedControl.swift
// PhoachingWithDB
//
// Created by Artem Vekshin on 14.07.2023.
//
import SwiftUI
struct SegmentedControlView: View {
@Binding private var selectedIndex: Int
@State private var frames: Array<CGRect>
@State private var backgroundFrame = CGRect.zero
@State private var isScrollable = true
private let titles: [String]
init(selectedIndex: Binding<Int>, titles: [String]) {
self._selectedIndex = selectedIndex
self.titles = titles
frames = Array<CGRect>(repeating: .zero, count: titles.count)
}
var body: some View {
VStack {
if isScrollable {
ScrollView(.horizontal, showsIndicators: false) {
SegmentedControlButtonView(selectedIndex: $selectedIndex, frames: $frames, backgroundFrame: $backgroundFrame, isScrollable: $isScrollable, checkIsScrollable: checkIsScrollable, titles: titles)
}
} else {
SegmentedControlButtonView(selectedIndex: $selectedIndex, frames: $frames, backgroundFrame: $backgroundFrame, isScrollable: $isScrollable, checkIsScrollable: checkIsScrollable, titles: titles)
}
}
.background(
GeometryReader { geoReader in
Color.clear.preference(key: RectPreferenceKey.self, value: geoReader.frame(in: .global))
.onPreferenceChange(RectPreferenceKey.self) {
self.setBackgroundFrame(frame: $0)
}
}
)
}
private func setBackgroundFrame(frame: CGRect)
{
backgroundFrame = frame
checkIsScrollable()
}
private func checkIsScrollable()
{
if frames[frames.count - 1].width > .zero
{
var width = CGFloat.zero
for frame in frames
{
width += frame.width
}
if isScrollable && width <= backgroundFrame.width
{
isScrollable = false
}
else if !isScrollable && width > backgroundFrame.width
{
isScrollable = true
}
}
}
}
private struct SegmentedControlButtonView: View {
@Binding private var selectedIndex: Int
@Binding private var frames: [CGRect]
@Binding private var backgroundFrame: CGRect
@Binding private var isScrollable: Bool
private let titles: [String]
let checkIsScrollable: (() -> Void)
init(selectedIndex: Binding<Int>, frames: Binding<[CGRect]>, backgroundFrame: Binding<CGRect>, isScrollable: Binding<Bool>, checkIsScrollable: (@escaping () -> Void), titles: [String])
{
_selectedIndex = selectedIndex
_frames = frames
_backgroundFrame = backgroundFrame
_isScrollable = isScrollable
self.checkIsScrollable = checkIsScrollable
self.titles = titles
}
var body: some View {
HStack(spacing: 0) {
ForEach(titles.indices, id: \.self) { index in
Button(action:{ selectedIndex = index })
{
HStack {
Text(titles[index])
.frame(height: 42)
.font(.custom("Unbounded-VariableFont_wght.ttf", size: 24))
}
}
.buttonStyle(CustomSegmentButtonStyle())
.background(
GeometryReader { geoReader in
Color.clear.preference(key: RectPreferenceKey.self, value: geoReader.frame(in: .global))
.onPreferenceChange(RectPreferenceKey.self) {
self.setFrame(index: index, frame: $0)
}
}
)
}
}
.modifier(UnderlineModifier(selectedIndex: selectedIndex, frames: frames))
}
private func setFrame(index: Int, frame: CGRect) {
self.frames[index] = frame
checkIsScrollable()
}
}
private struct CustomSegmentButtonStyle: ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
configuration
.label
.padding(EdgeInsets(top: 14, leading: 50, bottom: 14, trailing: 50 ))
}
}
struct RectPreferenceKey: PreferenceKey
{
typealias Value = CGRect
static var defaultValue = CGRect.zero
static func reduce(value: inout CGRect, nextValue: () -> CGRect)
{
value = nextValue()
}
}
struct UnderlineModifier: ViewModifier
{
var selectedIndex: Int
let frames: [CGRect]
func body(content: Content) -> some View
{
content
.background(
Rectangle()
.fill(Color.blue)
.frame(width: frames[selectedIndex].width, height: 2)
.offset(x: frames[selectedIndex].minX - frames[0].minX), alignment: .bottomLeading
)
.background(
Rectangle()
.fill(Color.white)
.frame(height: 1), alignment: .bottomLeading
)
.animation(.default)
}
}