-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownArrow.swift
74 lines (62 loc) · 2.29 KB
/
DownArrow.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
//
// DownArrow.swift
// SSSwiftUIAnimations
//
// Created by Mansi Prajapati on 06/05/24.
//
import SwiftUI
struct DownArrow: Shape {
// MARK: - Variables
var initialAnim: CGFloat
var isDownward: Bool
var progress: Float
var circleSize: CGFloat
var isAnimating: Bool
var bounceEffect: CGFloat
var animatableData: AnimatablePair<CGFloat, CGFloat> {
get { AnimatablePair(initialAnim, bounceEffect)}
set {
self.initialAnim = newValue.first
self.bounceEffect = newValue.second
}
}
func path(in rect: CGRect) -> Path {
var path = Path()
let startX = rect.midX - circleSize * (isAnimating ? 0.12 : 0.13)
let endX = rect.midX + circleSize * (isAnimating ? 0.12 : 0.12)
let centerY = rect.midY
var controlY: CGFloat
if isDownward {
controlY = centerY + (initialAnim * (rect.height / 2)) * (bounceEffect)
} else {
let bounceProgress = bounce(initialAnim)
controlY = centerY - (bounceProgress * (rect.height / 4)) - (bounceEffect * 6)
}
let controlX = rect.midX
path.move(to: CGPoint(x: startX, y: centerY))
if !isDownward {
// Curve for bounce effect
path.move(to: CGPoint(x: startX - circleSize * 0.1, y: centerY))
path.addQuadCurve(
to: CGPoint(x: rect.maxX + circleSize * 0.21, y: centerY),
control: CGPoint(x: controlX, y: controlY)
)
} else if initialAnim <= 1.0 {
// Down arrow
path.addLine(to: CGPoint(x: rect.midX, y: controlY))
path.addLine(to: CGPoint(x: endX, y: centerY))
} else if progress < 0.1 {
// Straight line
path.move(to: CGPoint(x: startX - circleSize * 0.1, y: centerY))
path.addLine(to: CGPoint(x: rect.maxX + circleSize * 0.21, y: centerY))
}
return path
}
// MARK: - Private functions
// Function to apply bounce effect
private func bounce(_ progress: CGFloat) -> CGFloat {
let numberOfBounces = 1
let bounces = CGFloat(numberOfBounces)
return abs((circleSize * 0.01 - progress) * sin(progress * .pi * bounces))
}
}