Skip to content

Commit 7b86d9f

Browse files
author
Simon Bachmann
authored
Merge pull request #13 from cs4alhaider/master
Added View extension to make it easier to implement it in any view
2 parents b0ae15a + 5b6caf2 commit 7b86d9f

7 files changed

+93
-8
lines changed

Diff for: Package.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ let package = Package(
2626
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2727
.target(
2828
name: "ConfettiSwiftUI",
29-
dependencies: []),
29+
dependencies: [],
30+
path: "Sources"),
3031
.testTarget(
3132
name: "ConfettiSwiftUITests",
32-
dependencies: ["ConfettiSwiftUI"]),
33+
dependencies: ["ConfettiSwiftUI"],
34+
path: "Tests"),
35+
3336
]
3437
)

Diff for: README.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,21 @@ If you prefer not to use any of dependency managers, you can integrate `Confetti
7979
First, add `import ConfettiSwiftUI` on every `swift` file you would like to use `ConfettiSwiftUI`. Define a integer as a state varable which is responsible for triggering the animation. Any change to that variable will span a new animation (increment and decrement).
8080

8181
```swift
82+
8283
import SwiftUI
83-
import ConfettiSwiftUI
8484

8585
struct ContentView: View {
86-
@State var counter:Int = 0
87-
86+
87+
@State private var counter: Int = 0
88+
8889
var body: some View {
89-
ZStack{
90-
Text("🎉").font(.system(size: 50)).onTapGesture(){counter += 1}
91-
ConfettiCannon(counter: $counter)
90+
Button("🎉") {
91+
counter += 1
9292
}
93+
.confettiCannon(counter: $counter)
9394
}
9495
}
96+
9597
```
9698

9799
### Parameters
File renamed without changes.
File renamed without changes.

Diff for: Sources/View+ConfettiCannon.swift

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// View+ConfettiCannon.swift
3+
//
4+
//
5+
// Created by Abdullah Alhaider on 24/03/2022.
6+
//
7+
8+
import SwiftUI
9+
10+
public extension View {
11+
12+
/// renders configurable confetti animaiton
13+
///
14+
/// - Usage:
15+
///
16+
/// ```
17+
/// import SwiftUI
18+
///
19+
/// struct ContentView: View {
20+
///
21+
/// @State private var counter: Int = 0
22+
///
23+
/// var body: some View {
24+
/// Button("Wow") {
25+
/// counter += 1
26+
/// }
27+
/// .confettiCannon(counter: $counter)
28+
/// }
29+
/// }
30+
/// ```
31+
///
32+
/// - Parameters:
33+
/// - counter: on any change of this variable the animation is run
34+
/// - num: amount of confettis
35+
/// - colors: list of colors that is applied to the default shapes
36+
/// - confettiSize: size that confettis and emojis are scaled to
37+
/// - rainHeight: vertical distance that confettis pass
38+
/// - fadesOut: reduce opacity towards the end of the animation
39+
/// - opacity: maximum opacity that is reached during the animation
40+
/// - openingAngle: boundary that defines the opening angle in degrees
41+
/// - closingAngle: boundary that defines the closing angle in degrees
42+
/// - radius: explosion radius
43+
/// - repetitions: number of repetitions of the explosion
44+
/// - repetitionInterval: duration between the repetitions
45+
///
46+
@ViewBuilder func confettiCannon(
47+
counter: Binding<Int>,
48+
num: Int = 20,
49+
confettis: [ConfettiType] = ConfettiType.allCases,
50+
colors: [Color] = [.blue, .red, .green, .yellow, .pink, .purple, .orange],
51+
confettiSize: CGFloat = 10.0,
52+
rainHeight: CGFloat = 600.0,
53+
fadesOut: Bool = true,
54+
opacity: Double = 1.0,
55+
openingAngle: Angle = .degrees(60),
56+
closingAngle: Angle = .degrees(120),
57+
radius: CGFloat = 300,
58+
repetitions: Int = 0,
59+
repetitionInterval: Double = 1.0
60+
) -> some View {
61+
ZStack {
62+
self
63+
ConfettiCannon(
64+
counter: counter,
65+
num: num,
66+
confettis: confettis,
67+
colors: colors,
68+
confettiSize: confettiSize,
69+
rainHeight: rainHeight,
70+
fadesOut: fadesOut,
71+
opacity: opacity,
72+
openingAngle: openingAngle,
73+
closingAngle: closingAngle,
74+
radius: radius,
75+
repetitions: repetitions,
76+
repetitionInterval: repetitionInterval
77+
)
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)