-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireworkCenterView.swift
More file actions
206 lines (187 loc) · 6.31 KB
/
Copy pathFireworkCenterView.swift
File metadata and controls
206 lines (187 loc) · 6.31 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// FireworkCenterView.swift
// AnimationExamples
//
// Created by Esat Gözcü on 2023/03/26.
//
import SwiftUI
struct FireworkCenterView: View{
@State var counter = 0
var body: some View{
ZStack{
Text("🎉").onTapGesture {
counter += 1
}.padding(.bottom, 40)
FireworkView(counter: $counter)
}.frame(
maxWidth: .infinity,
maxHeight: .infinity,
alignment: .bottom
).background(Color.black)
.ignoresSafeArea()
}
}
struct FireworkView: View{
@Binding var counter: Int
@StateObject var vm = FireworkCenterVM()
@State var animate = 0
@State var finishedAnimationCounter = 0
@State var firstAppear = false
var body: some View{
ZStack{
ForEach(finishedAnimationCounter..<animate, id:\.self){ i in
FireworkContainer(
vm: vm,
finishedAnimationCounter: $finishedAnimationCounter
)
}
}
.onAppear(){
firstAppear = true
}
.onChange(of: counter){value in
if firstAppear{
for i in 0...vm.repetitions{
DispatchQueue.main.asyncAfter(deadline: .now() + vm.repetitionInterval * Double(i)) {
animate += 1
}
}
}
}
}
}
struct FireworkContainer: View {
@StateObject var vm: FireworkCenterVM
@Binding var finishedAnimationCounter:Int
@State var firstAppear = true
@State var randomX = Double.random(in: -100...100)
@State var randomY = Double.random(in: 200.0...UIScreen.main.bounds.size.height-200)
@State var location: CGPoint = CGPoint(x: 0, y: 0)
@State var withPath = Int.random(in: 0...1)
var body: some View{
ZStack{
ForEach(0..<vm.pieceCount, id:\.self){ i in
FireworkFrame(
vm: vm,
index: i,
launchHeight: randomY,
color: getColor(),
withPath: withPath,
duration: getExplosionAnimationDuration()
)
}
}
.offset(x: location.x, y: location.y)
.onAppear(){
if firstAppear{
withAnimation(Animation.timingCurve(0.075, 0.690, 0.330, 0.870, duration: vm.launchAnimDuration).repeatCount(1)) {
location.x = randomX
location.y = -randomY
}
DispatchQueue.main.asyncAfter(deadline: .now() + getAnimDuration()) {
//Clear
self.finishedAnimationCounter += 1
}
firstAppear = false
}
}
}
func getAnimDuration() -> CGFloat{
return vm.explosionAnimationDuration + vm.launchAnimDuration
}
func getColor() -> Color{
return vm.colors.randomElement()!
}
func getRandomExplosionTimeVariation() -> CGFloat {
return CGFloat((0...999).randomElement()!) / 2100
}
func getExplosionAnimationDuration() -> CGFloat {
return vm.explosionAnimationDuration + getRandomExplosionTimeVariation()
}
}
struct FireworkFrame: View{
@StateObject var vm: FireworkCenterVM
@State var location: CGPoint = CGPoint(x: 0, y: 0)
@State var index: Int
@State var launchHeight: CGFloat
@State var percentage: CGFloat = 0.0
@State var strokeWidth: CGFloat = 2.0
@State var color: Color
@State var withPath: Int
@State var duration: CGFloat
var body: some View{
ZStack{
FireworkItem(vm: vm, shape: getShape(), size: vm.pieceSize, color: color)
.offset(x: location.x, y: location.y)
.onAppear{
withAnimation(
Animation
.timingCurve(0.0, 1.0, 1.0, 1.0, duration: duration)
.delay(vm.launchAnimDuration).repeatCount(1)
){
location.x = getDistance() * cos(deg2rad(getRandomAngle()))
location.y = -getDistance() * sin(deg2rad(getRandomAngle()))
}
}
if withPath == 0{
Path { path in
path.move(to: .zero)
path.addLine(
to: CGPoint(
x: getDistance() * cos(deg2rad(getRandomAngle())),
y: -getDistance() * sin(deg2rad(getRandomAngle()))
)
)
}.trim(from: 0.0, to: percentage)
.stroke(color, lineWidth: strokeWidth)
.frame(width: 1.0, height: 1.0)
.onAppear{
withAnimation(
Animation
.timingCurve(0.0, 1.0, 1.0, 1.0, duration: duration)
.delay(vm.launchAnimDuration).repeatCount(1)
){
percentage = 1.0
strokeWidth = 0.0
}
}
}
}
}
func getRandomAngle() -> CGFloat{
return (360.0 / Double(vm.pieceCount)) * Double(index)
}
func getShape() -> AnyView {
return vm.getShapes().randomElement()!
}
func getDistance() -> CGFloat {
return vm.radius + (launchHeight / 10)
}
func deg2rad(_ number: CGFloat) -> CGFloat {
return number * CGFloat.pi / 180
}
}
struct FireworkItem: View {
@StateObject var vm: FireworkCenterVM
@State var shape: AnyView
@State var size: CGFloat
@State var color: Color
@State var scale = 1.0
@State var move = false
var body: some View {
shape
.frame(width: size, height: size)
.scaleEffect(scale)
.foregroundColor(color)
.onAppear() {
withAnimation(
Animation
.linear(duration: vm.explosionAnimationDuration)
.delay(vm.launchAnimDuration)
.repeatCount(1)
) {
scale = 0.0
}
}
}
}