|
| 1 | +// |
| 2 | +// Copyright (c) 2025 Related Code - https://relatedcode.com |
| 3 | +// |
| 4 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 7 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 8 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 9 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 10 | +// THE SOFTWARE. |
| 11 | + |
| 12 | +import SwiftUI |
| 13 | + |
| 14 | +// MARK: - BallVerticalBounceView |
| 15 | +struct BallVerticalBounceView: View { |
| 16 | + |
| 17 | + // MARK: - Properties |
| 18 | + @State private var hud = ProgressHUD.shared |
| 19 | + |
| 20 | + // MARK: - Body |
| 21 | + var body: some View { |
| 22 | + GeometryReader { geometry in |
| 23 | + let size = geometry.size |
| 24 | + let width = size.width |
| 25 | + let height = size.height |
| 26 | + |
| 27 | + let ballRadius = width * 0.12 |
| 28 | + let lineWidth = width * 0.96 |
| 29 | + let lineY = height * 0.75 |
| 30 | + let topY = height * 0.15 |
| 31 | + |
| 32 | + let fallDistance = (lineY - ballRadius) - topY |
| 33 | + let maxBend = height * 0.2 |
| 34 | + |
| 35 | + let animationDuration: TimeInterval = 0.9 |
| 36 | + |
| 37 | + KeyframeAnimator(initialValue: AnimationPhase(t: 0), repeating: true) { phase in |
| 38 | + let t = phase.t |
| 39 | + let (yOffset, bend) = calculatePhysics(t: t, maxFall: fallDistance, maxBend: maxBend) |
| 40 | + |
| 41 | + let rubberHeight = maxBend * 1.5 |
| 42 | + |
| 43 | + ZStack { |
| 44 | + RubberBand(bend: bend) |
| 45 | + .stroke(hud.colorAnimation, style: StrokeStyle(lineWidth: width * 0.08, lineCap: .round)) |
| 46 | + .frame(width: lineWidth, height: rubberHeight) |
| 47 | + .position(x: width / 2, y: lineY + (rubberHeight / 2)) |
| 48 | + |
| 49 | + Circle() |
| 50 | + .fill(hud.colorAnimation) |
| 51 | + .frame(width: ballRadius * 2, height: ballRadius * 2) |
| 52 | + .position( |
| 53 | + x: width / 2, |
| 54 | + y: topY + yOffset |
| 55 | + ) |
| 56 | + } |
| 57 | + } keyframes: { _ in |
| 58 | + KeyframeTrack(\.t) { |
| 59 | + LinearKeyframe(1.0, duration: animationDuration) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // MARK: - Private Methods |
| 66 | + private func calculatePhysics(t: Double, maxFall: CGFloat, maxBend: CGFloat) -> (CGFloat, CGFloat) { |
| 67 | + let fallEnd = 0.40 |
| 68 | + let bendEnd = 0.60 |
| 69 | + |
| 70 | + var yOffset: CGFloat = 0 |
| 71 | + var currentBend: CGFloat = 0 |
| 72 | + |
| 73 | + if t < fallEnd { |
| 74 | + let nt = t / fallEnd |
| 75 | + let progress = nt * nt |
| 76 | + yOffset = maxFall * CGFloat(progress) |
| 77 | + currentBend = 0 |
| 78 | + |
| 79 | + } else if t < bendEnd { |
| 80 | + let nt = (t - fallEnd) / (bendEnd - fallEnd) |
| 81 | + |
| 82 | + let bendProgress = sin(nt * .pi) |
| 83 | + currentBend = maxBend * CGFloat(bendProgress) |
| 84 | + |
| 85 | + yOffset = maxFall + currentBend |
| 86 | + |
| 87 | + } else { |
| 88 | + let nt = (t - bendEnd) / (1.0 - bendEnd) |
| 89 | + |
| 90 | + let risingT = 1.0 - nt |
| 91 | + let progress = risingT * risingT |
| 92 | + |
| 93 | + yOffset = maxFall * CGFloat(progress) |
| 94 | + currentBend = 0 |
| 95 | + } |
| 96 | + |
| 97 | + return (yOffset, currentBend) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +private struct AnimationPhase { |
| 102 | + var t: Double |
| 103 | +} |
| 104 | + |
| 105 | +private struct RubberBand: Shape { |
| 106 | + var bend: CGFloat |
| 107 | + |
| 108 | + var animatableData: CGFloat { |
| 109 | + get { bend } |
| 110 | + set { bend = newValue } |
| 111 | + } |
| 112 | + |
| 113 | + func path(in rect: CGRect) -> Path { |
| 114 | + var path = Path() |
| 115 | + let start = CGPoint(x: 0, y: 0) |
| 116 | + let end = CGPoint(x: rect.width, y: 0) |
| 117 | + |
| 118 | + let control = CGPoint(x: rect.width / 2, y: bend * 2) |
| 119 | + |
| 120 | + path.move(to: start) |
| 121 | + path.addQuadCurve(to: end, control: control) |
| 122 | + |
| 123 | + return path |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +#Preview { |
| 128 | + BallVerticalBounceView() |
| 129 | + .frame(width: 70, height: 70) |
| 130 | +} |
0 commit comments