-
Notifications
You must be signed in to change notification settings - Fork 4
/
2.Spotify.swift
146 lines (120 loc) 路 3.62 KB
/
2.Spotify.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
//
// 2.Spotify.swift
// SUIChallenges
//
// Created by Alexander Kraev on 04.08.2022.
//
import SwiftUI
struct Spotify: View {
@State private var color: Color = Color.green
@State private var frequency: CGFloat = 0.5
@State private var columns: Int = 3
var body: some View {
ZStack {
VStack(spacing: 16.0) {
ColorPicker("", selection: $color)
HStack {
Text("Frequency: \(String(format: "%.1f", frequency))")
.font(.title3)
Stepper("", onIncrement: {
frequency += 0.1
}, onDecrement: {
if frequency > 0.2 {
frequency -= 0.1
}
})
}
HStack {
Text("Columns: \(columns)")
.font(.title3)
Stepper("", onIncrement: {
columns += 1
}, onDecrement: {
if columns > 2 {
columns -= 1
}
})
}
}
.vTop()
.hTrailing()
.padding(20.0)
.zIndex(1)
HStack {
ForEach(0...columns, id: \.self) { _ in
Bar(color: color, interval: frequency)
}
}
.frame(width: 170, height: 170)
}
}
}
fileprivate struct Bar: View {
let color: Color
let timer: Timer.TimerPublisher
@State private var paddingTop: CGFloat = 1.0
init(color: Color, interval: TimeInterval) {
self.color = color
timer = Timer.publish(every: interval, on: .main, in: .common)
}
var body: some View {
Segment(color: color, paddingTop: paddingTop)
.animation(.spring(), value: paddingTop)
.onReceive(timer.autoconnect()) { _ in
paddingTop = CGFloat.random(in: 0...1)
}
}
}
fileprivate struct Segment: Animatable, View {
let color: Color
var paddingTop: CGFloat = 1.0
public var animatableData: CGFloat {
get { paddingTop }
set { paddingTop = newValue }
}
var body: some View {
GeometryReader { geometry in
RoundedRectangle(cornerRadius: 8.0)
.fill(color)
.padding(.top, geometry.size.height * paddingTop)
}
}
}
fileprivate extension View {
// MARK: Vertical Center
func vCenter() -> some View {
self
.frame(maxHeight: .infinity, alignment: .center)
}
// MARK: Vertical Top
func vTop() -> some View {
self
.frame(maxHeight: .infinity, alignment: .top)
}
// MARK: Vertical Bottom
func vBottom() -> some View {
self
.frame(maxHeight: .infinity, alignment: .bottom)
}
// MARK: Horizontal Center
func hCenter() -> some View {
self
.frame(maxWidth: .infinity, alignment: .center)
}
// MARK: Horizontal Leading
func hLeading() -> some View {
self
.frame(maxWidth: .infinity, alignment: .leading)
}
// MARK: Horizontal Trailing
func hTrailing() -> some View {
self
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
struct Spotify_Previews: PreviewProvider {
static var previews: some View {
Spotify()
.preferredColorScheme(.dark)
}
}