|
1 | 1 | import SwiftUI |
2 | 2 |
|
3 | 3 | struct PlayingIndicator: View { |
4 | | - @State private var isAnimating = false |
| 4 | + @State private var animationPhases: [CGFloat] = [0, 0, 0] |
| 5 | + // Update 60 times per second for smooth animation |
| 6 | + private let timer = Timer.publish(every: 1.0/60.0, on: .main, in: .common).autoconnect() |
5 | 7 |
|
6 | 8 | var body: some View { |
7 | 9 | HStack(spacing: 2) { |
8 | 10 | ForEach(0..<3) { index in |
9 | 11 | RoundedRectangle(cornerRadius: 1) |
10 | 12 | .fill(Color.accentColor) |
11 | | - .frame(width: 3, height: isAnimating ? 12 : 4) |
12 | | - .animation( |
13 | | - Animation.easeInOut(duration: 0.4) |
14 | | - .repeatForever(autoreverses: true) |
15 | | - .delay(Double(index) * 0.1), |
16 | | - value: isAnimating |
17 | | - ) |
| 13 | + .frame(width: 3, height: barHeight(for: index)) |
| 14 | + .animation(.none) // Disable implicit animations for smoother manual control |
18 | 15 | } |
19 | 16 | } |
20 | 17 | .frame(width: 16, height: 12) |
21 | 18 | .clipped() |
22 | | - .onAppear { |
23 | | - isAnimating = true |
24 | | - } |
25 | | - .onDisappear { |
26 | | - isAnimating = false |
| 19 | + .onReceive(timer) { _ in |
| 20 | + updateAnimation() |
27 | 21 | } |
28 | 22 | } |
| 23 | + |
| 24 | + private func barHeight(for index: Int) -> CGFloat { |
| 25 | + let phase = animationPhases[index] |
| 26 | + // Smoother sine wave calculation |
| 27 | + let height = 6 + (6 * sin(phase)) |
| 28 | + return max(2, height) // Ensure minimum height of 2 |
| 29 | + } |
| 30 | + |
| 31 | + private func updateAnimation() { |
| 32 | + // Slower, smoother animation with different speeds for each bar |
| 33 | + animationPhases[0] += 0.08 |
| 34 | + animationPhases[1] += 0.10 |
| 35 | + animationPhases[2] += 0.12 |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#Preview { |
| 40 | + PlayingIndicator() |
| 41 | + .padding() |
| 42 | + .background(Color.gray.opacity(0.1)) |
29 | 43 | } |
0 commit comments