-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfettiCenterView.swift
More file actions
186 lines (165 loc) · 7.06 KB
/
Copy pathConfettiCenterView.swift
File metadata and controls
186 lines (165 loc) · 7.06 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// ConfettiView.swift
// AnimationExamples
//
// Created by Esat Gözcü on 2023/03/17.
//
import SwiftUI
struct ConfettiCenterView: View{
@State var counter = 0
var body: some View{
ZStack{
Text("🎉").onTapGesture {
counter += 1
}
/* FireworkEffect */
/*ConfettiView(counter: $counter,
confettiVM: ConfettiVM(confettiNumber: 40,
fireworkEffect: true,
openingAngle: Angle(degrees: 0),
closingAngle: Angle(degrees: 360),
radius: 200.0))*/
/* Emoji - Text */
/*ConfettiView(counter: $counter, confettiVM: ConfettiVM(confettiNumber: 10,
confettiTypes: [
.text("❤️"),
.text("💙"),
.text("A")],
confettiSize: 30))*/
/* Image */
/*ConfettiView(counter: $counter, confettiVM: ConfettiVM(confettiNumber: 10,
confettiTypes: [
.sfSymbol(symbolName: "star.fill"),
.sfSymbol(symbolName: "arrow.triangle.2.circlepath"),
.sfSymbol(symbolName: "square.and.arrow.up")],
confettiSize: 30, explosionAnimDuration: 0.7))*/
/* Default */
ConfettiView(counter: $counter)
}
}
}
struct ConfettiView: View{
@Binding var counter: Int
@StateObject var confettiVM = ConfettiCenterVM()
@State var animate = 0
@State var finishedAnimationCounter = 0
@State var firstAppear = false
var body: some View{
ZStack{
ForEach(finishedAnimationCounter..<animate, id:\.self){ i in
ConfettiContainer(
confettiVM: confettiVM, finishedAnimationCounter: $finishedAnimationCounter
)
}
}
.onAppear(){
firstAppear = true
}
.onChange(of: counter){value in
if firstAppear{
for i in 0...confettiVM.repetitions{
DispatchQueue.main.asyncAfter(deadline: .now() + confettiVM.repetitionInterval * Double(i)) {
animate += 1
}
}
}
}
}
}
struct ConfettiContainer: View {
@StateObject var confettiVM: ConfettiCenterVM
@Binding var finishedAnimationCounter:Int
@State var firstAppear = true
var body: some View{
ZStack{
ForEach(0..<confettiVM.confettiNumber, id:\.self){_ in
ConfettiFrame(confettiVM: confettiVM)
}
}
.onAppear(){
if firstAppear{
DispatchQueue.main.asyncAfter(deadline: .now() + confettiVM.getAnimDuration()) {
self.finishedAnimationCounter += 1
}
firstAppear = false
}
}
}
}
struct ConfettiFrame: View{
//For Animation.timingCurve
//https://matthewlein.com/tools/ceaser
@StateObject var confettiVM: ConfettiCenterVM
@State var location: CGPoint = CGPoint(x: 0, y: 0)
@State var opacity: Double = 0.0
var body: some View{
ConfettiItem(shape: getShape(), color: getColor())
.offset(x: location.x, y: location.y)
.opacity(opacity)
.onAppear(){
withAnimation(Animation.timingCurve(0.1, 1.0, 0, 1, duration: getAnimationDuration())) {
opacity = confettiVM.opacity
let randomAngle:CGFloat
if confettiVM.openingAngle.degrees <= confettiVM.closingAngle.degrees{
randomAngle = CGFloat.random(in: CGFloat(confettiVM.openingAngle.degrees)...CGFloat(confettiVM.closingAngle.degrees))
}else{
randomAngle = CGFloat.random(in: CGFloat(confettiVM.openingAngle.degrees)...CGFloat(confettiVM.closingAngle.degrees + 360)).truncatingRemainder(dividingBy: 360)
}
let distance = getDistance()
location.x = distance * cos(deg2rad(randomAngle))
location.y = -distance * sin(deg2rad(randomAngle))
}
DispatchQueue.main.asyncAfter(deadline: .now() + getDelayBeforeDropAnimation()) {
withAnimation(Animation.timingCurve(0.12, 0, 0.39, 0, duration: confettiVM.dropAnimationDuration)) {
location.y += confettiVM.dropHeight
opacity = confettiVM.fadesOut ? 0 : confettiVM.opacity
}
}
}
}
func getShape() -> AnyView {
return confettiVM.getShapes().randomElement()!
}
func getColor() -> Color {
return confettiVM.colors.randomElement()!
}
func getRandomExplosionTimeVariation() -> CGFloat {
return CGFloat((0...999).randomElement()!) / 2100
}
func getAnimationDuration() -> CGFloat {
return 0.2 + confettiVM.explosionAnimationDuration + getRandomExplosionTimeVariation()
}
func getDistance() -> CGFloat {
if !confettiVM.fireworkEffect{
return pow(CGFloat.random(in: 0.01...1), 2.0/7.0) * confettiVM.radius
}
return confettiVM.radius
}
func getDelayBeforeDropAnimation() -> TimeInterval {
confettiVM.explosionAnimationDuration * 0.1
}
func deg2rad(_ number: CGFloat) -> CGFloat {
return number * CGFloat.pi / 180
}
}
struct ConfettiItem: View {
@State var shape: AnyView
@State var color: Color
@State var move = false
@State var anchor = CGFloat(Int.random(in: 0...1))
@State var spinDirX = [-1.0, 1.0].randomElement()!
@State var spinDirZ = [-1.0, 1.0].randomElement()!
@State var xSpeed = Double.random(in: 0.501...2.201)
@State var zSpeed = Double.random(in: 0.501...2.201)
var body: some View {
shape
.foregroundColor(color)
.rotation3DEffect(.degrees(move ? 360:0), axis: (x: spinDirX, y: 0, z: 0))
.animation(Animation.linear(duration: xSpeed).repeatForever(), value: move)
.rotation3DEffect(.degrees(move ? 360:0), axis: (x: 0, y: 0, z: spinDirZ), anchor: UnitPoint(x: anchor, y: anchor))
.animation(Animation.linear(duration: zSpeed).repeatForever(), value: move)
.onAppear() {
move = true
}
}
}