-
Notifications
You must be signed in to change notification settings - Fork 43
/
ReactionsViewInterpolatingSpringSD.swift
71 lines (55 loc) · 2.98 KB
/
ReactionsViewInterpolatingSpringSD.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
//
// ReactionsView.swift
// Learning SwiftUI Spring Animations: The Basics and Beyond
// Created by Amos from getstream.io
//
import SwiftUI
struct ReactionsView: View {
// 1. Define initial animation state
@State private var showReaction = false
let inboundBubbleColor = Color(#colorLiteral(red: 0.07058823529, green: 0.07843137255, blue: 0.0862745098, alpha: 1))
let reactionsBGColor = Color(#colorLiteral(red: 0.05490196078, green: 0.09019607843, blue: 0.137254902, alpha: 1))
var body: some View {
ZStack { // Reactions background
RoundedRectangle(cornerRadius: 28)
.frame(width: 216, height: 40)
.foregroundColor(Color(.systemGray6))
// 3. Add property to animate
.scaleEffect(CGFloat(showReaction ? 1 : 0), anchor: .topTrailing)
// 4. Add animation modifier
.animation(.interpolatingSpring(stiffness: 170, damping: 15).delay(0.05), value: showReaction)
// Reaction icons
HStack(spacing: 20) {
Image("heart")
.offset(x: showReaction ? 0 : -15)
.scaleEffect(showReaction ? 1 : 0, anchor: .bottomLeading)
.animation(.interpolatingSpring(stiffness: 170, damping: 8).delay(0.1), value: showReaction)
Image("thumbup")
.offset(x: showReaction ? 0 : -15)
.scaleEffect(showReaction ? 1 : 0, anchor: .bottom)
.rotationEffect(.degrees(showReaction ? 0 : -45))
.animation(.interpolatingSpring(stiffness: 170, damping: 8).delay(0.2), value: showReaction)
Image("thumbdown")
.scaleEffect(showReaction ? 1 : 0, anchor: .topTrailing)
.rotationEffect(.degrees(showReaction ? 0 : 45))
.animation(.interpolatingSpring(stiffness: 170, damping: 8).delay(0.3), value: showReaction)
Image("crying")
.scaleEffect(showReaction ? 1 : 0, anchor: .bottom)
.animation(.interpolatingSpring(stiffness: 170, damping: 8).delay(0.4), value: showReaction)
Image("sad")
.offset(x: showReaction ? 0 : 15)
.scaleEffect(showReaction ? 1 : 0, anchor: .bottomTrailing)
.animation(.interpolatingSpring(stiffness: 170, damping: 8).delay(0.5), value: showReaction)
}
} // All reaction views
.onAppear{
showReaction.toggle()
}
}
}
struct ReactionsView_Previews: PreviewProvider {
static var previews: some View {
ReactionsView()
.preferredColorScheme(.dark)
}
}