Skip to content

Commit 8cf11b3

Browse files
UNT-T11872 Bounce animation updated
1 parent aed362e commit 8cf11b3

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

SSSwiftUIAnimations/SSSwiftUIAnimations/Examples/ExampleProgressView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ struct ExampleProgressView: View {
2020
}, onProgressCompletion: {
2121
timer?.invalidate()
2222
}, onCancelProgress: {
23-
progress = 0.0
2423
timer?.invalidate()
2524
})
2625
}.navigationBarTitle("ProgressView Example", displayMode: .inline)

SSSwiftUIAnimations/SSSwiftUIAnimations/ProgressAnimation/ArrowView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct Arrow: View {
1313
var arrowStyle: ArrowViewParams
1414
var style: SSProgressViewStyle
1515
@Binding var progress: Float
16+
@Binding var bounceEffect: CGFloat
1617

1718
var body: some View {
1819
VStack(spacing: 0) {
@@ -25,7 +26,7 @@ struct Arrow: View {
2526
)
2627
.opacity(arrowStyle.showVerticalLine ? 1 : 0)
2728
.foregroundColor(style.arrowColor)
28-
.offset(y: arrowStyle.isAnimating ? (!arrowStyle.animationStarted ? -5 : -(style.circleSize/2)) : 0)
29+
.offset(y: arrowStyle.isAnimating ? (!arrowStyle.animationStarted ? -10 : -(style.circleSize/2)) : 0)
2930
.animation(Animation.linear.speed(!arrowStyle.animationStarted ? 100 : .zero), value: 0)
3031

3132
if progress < 0.1 {
@@ -36,7 +37,7 @@ struct Arrow: View {
3637
isDownward: arrowStyle.isDownward,
3738
progress: Float(progress),
3839
circleSize: style.circleSize,
39-
isAnimating: arrowStyle.isAnimating
40+
isAnimating: arrowStyle.isAnimating, bounceEffect: bounceEffect
4041
)
4142
.stroke(style: StrokeStyle(lineWidth: style.arrowStrokeWidth, lineCap: .round))
4243
.frame(width: 10, height: 50)

SSSwiftUIAnimations/SSSwiftUIAnimations/ProgressAnimation/DownArrow.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ struct DownArrow: Shape {
1515
var progress: Float
1616
var circleSize: CGFloat
1717
var isAnimating: Bool
18+
var bounceEffect: CGFloat
1819

19-
// MARK: - Animatable Data
20-
var animatableData: CGFloat {
21-
get { initialAnim }
22-
set { initialAnim = newValue }
20+
var animatableData: AnimatablePair<CGFloat, CGFloat> {
21+
get { AnimatablePair(initialAnim, bounceEffect)}
22+
23+
set {
24+
self.initialAnim = newValue.first
25+
self.bounceEffect = newValue.second
26+
}
2327
}
2428

2529
func path(in rect: CGRect) -> Path {
@@ -30,10 +34,10 @@ struct DownArrow: Shape {
3034
var controlY: CGFloat
3135

3236
if isDownward {
33-
controlY = centerY + (initialAnim * (rect.height / 2))
37+
controlY = centerY + (initialAnim * (rect.height / 2)) * (bounceEffect)
3438
} else {
3539
let bounceProgress = bounce(initialAnim)
36-
controlY = centerY - (bounceProgress * (rect.height / 4))
40+
controlY = centerY - (bounceProgress * (rect.height / 4)) - (bounceEffect * 6)
3741
}
3842

3943
let controlX = rect.midX
@@ -62,8 +66,9 @@ struct DownArrow: Shape {
6266

6367
// Function to apply bounce effect
6468
private func bounce(_ progress: CGFloat) -> CGFloat {
65-
let numberOfBounces = 2
69+
let numberOfBounces = 1
6670
let bounces = CGFloat(numberOfBounces)
6771
return abs((circleSize * 0.01 - progress) * sin(progress * .pi * bounces))
6872
}
6973
}
74+

SSSwiftUIAnimations/SSSwiftUIAnimations/ProgressAnimation/ProgressView.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct SSProgressView: View {
5555
// Call back for restarting animation in between
5656
var onCancelProgress: (() -> Void)?
5757

58+
@State var bounceEffect: CGFloat = 1.0
59+
5860
var body: some View {
5961
ZStack {
6062
// Progress Circle with animation
@@ -68,7 +70,7 @@ struct SSProgressView: View {
6870
let arrowStyle = ArrowViewParams(isAnimating: isAnimating, progress: Double(progress), initialAnim: initialAnim, isDownward: isDownArrow, animationStarted: startDotAnim, showVerticalLine: showVerticalLine)
6971

7072
// Arrow view
71-
Arrow(arrowStyle: arrowStyle, style: style, progress: $progress)
73+
Arrow(arrowStyle: arrowStyle, style: style, progress: $progress, bounceEffect: $bounceEffect)
7274
}.allowsHitTesting(canTap)
7375
.onTapGesture {
7476
if style.allowCancelProgress && !isReset {
@@ -103,6 +105,12 @@ struct SSProgressView: View {
103105
}
104106
}
105107

108+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
109+
withAnimation(.linear(duration: 0.7)) {
110+
bounceEffect = 2
111+
}
112+
}
113+
106114
// Allowing view to reset properly before starting the progress again
107115
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
108116
canTap = style.allowCancelProgress ? true : false
@@ -189,6 +197,7 @@ struct SSProgressView: View {
189197
isAnimating = false
190198
initialAnim = 1.0
191199
isDownArrow = true
200+
bounceEffect = 1.0
192201
startDotAnim = false
193202
showVerticalLine = true
194203
showPercent = false

0 commit comments

Comments
 (0)