-
Notifications
You must be signed in to change notification settings - Fork 42
/
DampingFractionBounce.swift
64 lines (56 loc) · 2.19 KB
/
DampingFractionBounce.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
//
// DampingFractionBounce.swift
// An interpolating spring with stiffness and damping as parameters
//
// Created by Amos from getstream.io
//
import SwiftUI
struct DampingFractionBounce: View {
@State private var moving = false
var body: some View {
VStack(alignment: .trailing) {
// Ball 1, DF = Damping Fraction
HStack {
Text("High bounce: DF = 0.2")
Image("ball")
.offset(x: moving ? 0 : 100)
// High bounce: Damping fraction = 0.2
.animation(.spring(response: 0.55, dampingFraction: 0.2, blendDuration: 0.0).repeatForever(autoreverses: false), value: moving)
}
// Ball 2
HStack {
Text("Medium bounce: DF = 0.5")
Image("ball")
.offset(x: moving ? 0 : 100)
// Medium bounce: Damping fraction = 0.5
.animation(.spring(response: 0.55, dampingFraction: 0.5, blendDuration: 0.0).repeatForever(autoreverses: false), value: moving)
}
// Ball 3
HStack {
Text("Low bounce: DF = 0.75")
Image("ball")
.offset(x: moving ? 0 : 100)
// Low bounce: Damping fraction = 0.75
.animation(.spring(response: 0.55, dampingFraction: 0.75, blendDuration: 0.0).repeatForever(autoreverses: false), value: moving)
}
// Ball 4
HStack {
Text("No bounce: DF = 1")
Image("ball")
.offset(x: moving ? 0 : 100)
// No bounce: Damping fraction = 1
.animation(.spring(response: 0.55, dampingFraction: 1, blendDuration: 0.0).repeatForever(autoreverses: false), value: moving)
}
} // All balls
.onAppear{
moving.toggle()
}
}
}
struct DampingFractionBounce_Previews: PreviewProvider {
static var previews: some View {
DampingFractionBounce()
.preferredColorScheme(.dark)
.previewInterfaceOrientation(.landscapeLeft)
}
}