-
Notifications
You must be signed in to change notification settings - Fork 4
/
4.Firework.swift
134 lines (116 loc) 路 3.81 KB
/
4.Firework.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
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
//
// 4.Firework.swift
// SUIChallenges
//
// Created by Alexander Kraev on 23.08.2022.
//
import SwiftUI
struct FireworkParticlesGeometryEffect : GeometryEffect {
var time : Double
var speed = Double.random(in: 20 ... 200)
var direction = Double.random(in: -Double.pi ... Double.pi)
var animatableData: Double {
get { time }
set { time = newValue }
}
func effectValue(size: CGSize) -> ProjectionTransform {
let xTranslation = speed * cos(direction) * time
let yTranslation = speed * sin(direction) * time
let affineTranslation = CGAffineTransform(translationX: xTranslation, y: yTranslation)
return ProjectionTransform(affineTranslation)
}
}
struct ParticlesModifier: ViewModifier {
@State var time = 0.0
@State var scale = 0.1
let duration = Double.random(in: 3.0 ... 7.0)
func body(content: Content) -> some View {
ZStack {
ForEach(0..<80, id: \.self) { index in
content
.hueRotation(Angle(degrees: time * 80))
.scaleEffect(scale)
.modifier(FireworkParticlesGeometryEffect(time: time))
.opacity((duration - time) / duration)
.animation(.easeOut(duration: duration).repeatForever(autoreverses: false), value: scale)
}
}
.onAppear {
time = duration
scale = 1.0
}
}
}
struct Fireworks: View {
@State var scaling: Bool = false
var body: some View {
ZStack {
VStack {
VStack {
Text("Thanks so much!")
.font(.largeTitle)
Text("200")
.font(.system(size: 60))
.scaleEffect(scaling ? 1.1 : 1.0)
.animation(.easeOut(duration: 1.0).repeatForever(), value: scaling)
Text("I really appreciate you!")
.font(.largeTitle)
}
.padding()
.vTop()
}
.zIndex(1)
ForEach(0..<Int.random(in: 10...20), id: \.self) { _ in
Circle()
.fill(Color.blue)
.frame(width: 12, height: 12)
.modifier(ParticlesModifier())
.offset(x: CGFloat.random(in: -200...200), y : CGFloat.random(in: -200...200))
}
.zIndex(2)
Image("like-you")
.resizable()
.scaledToFit()
.frame(width: 400, height: 400)
.scaleEffect(scaling ? 1.2 : 1.0)
.animation(.easeOut(duration: 1.0).repeatForever(), value: scaling)
.vBottom()
.zIndex(3)
}
.onAppear {
scaling.toggle()
}
}
}
fileprivate extension View {
// MARK: Vertical Center
func vCenter() -> some View {
self
.frame(maxHeight: .infinity, alignment: .center)
}
// MARK: Vertical Top
func vTop() -> some View {
self
.frame(maxHeight: .infinity, alignment: .top)
}
// MARK: Vertical Bottom
func vBottom() -> some View {
self
.frame(maxHeight: .infinity, alignment: .bottom)
}
// MARK: Horizontal Center
func hCenter() -> some View {
self
.frame(maxWidth: .infinity, alignment: .center)
}
// MARK: Horizontal Leading
func hLeading() -> some View {
self
.frame(maxWidth: .infinity, alignment: .leading)
}
// MARK: Horizontal Trailing
func hTrailing() -> some View {
self
.frame(maxWidth: .infinity, alignment: .trailing)
}
}