-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFireworkCenterVM.swift
More file actions
105 lines (99 loc) · 3.35 KB
/
Copy pathFireworkCenterVM.swift
File metadata and controls
105 lines (99 loc) · 3.35 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
//
// FireworkCenterVM.swift
// AnimationExamples
//
// Created by Esat Gözcü on 2023/03/26.
//
import SwiftUI
/// - Parameters:
/// - pieceCount: amount of confetti
/// - colors: list of colors that is applied to the default shapes
/// - pieceSize: size that confetti and emojis are scaled to
/// - radius: explosion radius
/// - repetitions: number of repetitions of the explosion
/// - repetitionInterval: duration between the repetitions
class FireworkCenterVM: ObservableObject{
@Published var pieceCount: Int
@Published var pieceType: [FireworkType]
@Published var colors: [Color]
@Published var pieceSize: CGFloat
@Published var radius: CGFloat
@Published var repetitions: Int
@Published var repetitionInterval: Double
@Published var explosionAnimationDuration: Double
@Published var launchAnimDuration: Double
init(pieceCount: Int = 20,
pieceType: [FireworkType] = [.shape(.circle)],
colors: [Color] = [
Color.init(hex: "f88f22"),
Color.init(hex: "9c1d08"),
Color.init(hex: "ce7117"),
Color.init(hex: "f24d24"),
Color.init(hex: "113bc6"),
Color.init(hex: "c54a85"),
Color.init(hex: "92af96"),
Color.init(hex: "d23508")
],
pieceSize: CGFloat = 10.0,
radius: CGFloat = 100,
repetitions: Int = 0,
repetitionInterval: Double = 1.0,
explosionAnimDuration: Double = 1.2,
launchAnimDuration: Double = 3.0
) {
self.pieceCount = pieceCount
self.pieceType = pieceType
self.colors = colors
self.pieceSize = pieceSize
self.radius = radius
self.repetitions = repetitions
self.repetitionInterval = repetitionInterval
self.explosionAnimationDuration = explosionAnimDuration
self.launchAnimDuration = launchAnimDuration
}
func getShapes() -> [AnyView]{
var shapes = [AnyView]()
for firework in pieceType{
switch firework {
case .shape(_):
shapes.append(AnyView(firework.view.frame(width: pieceSize, height: pieceSize, alignment: .center)))
default:
shapes.append(AnyView(firework.view.font(.system(size: pieceSize))))
}
}
return shapes
}
}
public enum FireworkType: CaseIterable, Hashable {
public enum Shape {
case circle
case triangle
case square
case slimRectangle
case roundedCross
}
case shape(Shape)
case text(String)
case sfSymbol(symbolName: String)
public var view:AnyView{
switch self {
case .shape(.square):
return AnyView(Rectangle())
case .shape(.circle):
return AnyView(Circle())
case .shape(.triangle):
return AnyView(Triangle())
case .shape(.slimRectangle):
return AnyView(SlimRectangle())
case .shape(.roundedCross):
return AnyView(RoundedCross())
case let .text(text):
return AnyView(Text(text))
case .sfSymbol(let symbolName):
return AnyView(Image(systemName: symbolName))
}
}
public static var allCases: [FireworkType] {
return [.shape(.circle), .shape(.triangle), .shape(.square), .shape(.slimRectangle), .shape(.roundedCross)]
}
}