-
Notifications
You must be signed in to change notification settings - Fork 12
/
playful_button.swift
167 lines (149 loc) · 5.8 KB
/
playful_button.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
//
// AudioPlayerButton.swift
// whatsgoingon
//
// Created by Fabian Gruß on 06.10.23.
//
import SwiftUI
struct AudioPlayerButton: View {
// Default state
@State private var state: AudioPlayerState = .isIdle
// Vars for the timer
@State private var recordingStart: Date = .now
// For the example, let's go with 5 seconds duration. Use your own values from the actual audio files here
private var duration = 5.0
@State private var elapsedPlaybackTime = TimeInterval()
@State private var totalPausedTime = TimeInterval()
@State private var progress: CGFloat = 0
// Publish a timer to listen to for the counter
@State var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
// Colors (adjust to your liking
private var primaryColor = Color(hex: 0xff58a8) // Pink primary
private var secondaryColor = Color(hex: 0xfcdbeb) // Pink secondary
var body: some View {
Button {
// A little extra bounce for actions through the stateMachine
withAnimation(.bouncy(extraBounce: 0.2)) {
stateMachine()
}
} label: {
HStack {
Image(systemName: state == .isPlaying ? "pause.fill" : "play.fill").font(.system(size: 22))
.contentTransition(.symbolEffect(.replace))
.foregroundColor(primaryColor)
.padding(.leading, 12)
Spacer()
HStack {
Spacer()
Text(formatTimeInterval(interval: elapsedPlaybackTime))
.font(.caption)
.fontWeight(.semibold)
.fontDesign(.rounded)
.foregroundStyle(primaryColor)
.contentTransition(.opacity)
.padding(.trailing, 12)
.frame(width: 52, alignment: .leading)
}
.onReceive(timer) { firedDate in
// Only update UI if isPlaying
if state == .isPlaying {
let elapsedTime = firedDate.timeIntervalSince(recordingStart)
withAnimation {
progress = CGFloat(elapsedTime / 5.0)
}
elapsedPlaybackTime = TimeInterval(progress * 5)
// Stop the playing with a little less bounce
if elapsedTime > duration {
withAnimation(.bouncy(extraBounce: 0.1)) {
state = .isIdle
reset()
timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
}
}
}
}
}
.frame(width: state == .isIdle ? 96 : 196, height: 44)
.background(secondaryColor)
.clipShape(RoundedRectangle(cornerRadius: 12.0))
}
.buttonStyle(SquishyButton(color: secondaryColor, goalColor: primaryColor.opacity(0.3), minWidth: 96, minHeight: 44, cornerRadius: 12))
.padding(.all, 48)
}
private func stateMachine() {
switch state {
case .isIdle:
reset()
state = .isPlaying
case .isPlaying:
totalPausedTime += Date().timeIntervalSince(recordingStart)
timer.upstream.connect().cancel()
state = .isPaused
case .isPaused:
recordingStart = Date().addingTimeInterval(-totalPausedTime)
timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
state = .isPlaying
}
}
/// Formats a time interval to mm:ss
private func formatTimeInterval(interval: TimeInterval) -> String {
let duration: Duration = .seconds(interval)
return duration.formatted(.time(pattern: .minuteSecond(padMinuteToLength: 2))) // "02:06"
}
/// Reset timer values
private func reset() {
recordingStart = Date()
totalPausedTime = TimeInterval()
elapsedPlaybackTime = TimeInterval()
}
}
enum AudioPlayerState: String {
case isIdle, isPlaying, isPaused
}
#Preview {
AudioPlayerButton()
}
// Let's use hex colors
extension Color {
init(hex: UInt, alpha: Double = 1) {
self.init(
.sRGB,
red: Double((hex >> 16) & 0xff) / 255,
green: Double((hex >> 08) & 0xff) / 255,
blue: Double((hex >> 00) & 0xff) / 255,
opacity: alpha
)
}
}
// Button style
struct SquishyButton: ButtonStyle {
var color: Color
var goalColor: Color
var minWidth: Double = 98
var minHeight: Double = 70
var cornerRadius: Double = 18
func makeBody(configuration: Configuration) -> some View {
configuration.label
.frame(minWidth: minWidth, minHeight: minHeight)
.overlay {
Squircle(cornerRadius: configuration.isPressed ? (cornerRadius+1) : cornerRadius).fill(configuration.isPressed ? goalColor.opacity(0.4) : .clear)
}
.background(Squircle(cornerRadius: configuration.isPressed ? (cornerRadius+1) : cornerRadius))
.foregroundStyle(color)
.scaleEffect(configuration.isPressed ? 0.94 : 1.0)
.animation(.spring(duration: 0.5), value: configuration.isPressed)
.sensoryFeedback(.impact(intensity: 0.8), trigger: configuration.isPressed)
}
}
// Shape style
struct Squircle: Shape {
var cornerRadius: CGFloat
func path(in rect: CGRect) -> Path {
let path = UIBezierPath(
roundedRect: rect,
byRoundingCorners: .allCorners,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)
)
return Path(path.cgPath)
}
}