-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathCascadingTextAnimation.swift
56 lines (51 loc) · 1.74 KB
/
CascadingTextAnimation.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
//
// ChainedSpring.swift
// HundredDaysOfSwiftUI
//
/* ANIMATION PRINCIPLE: Overlapping Action
Overlapping Action/ Sequence and stagger:**
- Cascading and offsetting similar elements animating in the same way.
- Distribute their entrances over time instead of animating them at once
- Example: Cascading text animation
*/
//
import SwiftUI
struct CascadingTextAnimation: View {
let letters = Array("SpringAnimation")
@State private var enabled = false
@State private var dragAmount = CGSize.zero
var body: some View {
HStack(spacing: 0) {
ForEach(0..<letters.count, id: \.self) { num in
Text(String(letters[num]))
.padding(5)
.font(.title)
.background(enabled ? .blue : .red)
.hueRotation(.degrees(enabled ? 0 : 150))
.rotationEffect(.degrees(enabled ? 0 : 360), anchor: .bottomLeading)
.border(.white)
.cornerRadius(2)
.offset(dragAmount)
.animation(.interpolatingSpring(stiffness: 170, damping: 5).delay(Double(num) / 20), value: dragAmount)
}
}
.padding()
.gesture(
DragGesture()
.onChanged { dragAmount = $0.translation }
// _ ignore the value coming in this time
.onEnded { _ in
withAnimation{
dragAmount = .zero
enabled.toggle()
}
}
)
}
}
struct CascadingTextAnimation_Previews: PreviewProvider {
static var previews: some View {
CascadingTextAnimation()
.preferredColorScheme(.dark)
}
}