Compare commits

..

23 Commits

Author SHA1 Message Date
Simon Bachmann 2a5797c5ad
Update README.md 2022-05-14 15:53:37 +02:00
Simon Bachmann 2c51255034
Update README.md 2022-05-14 15:50:06 +02:00
Simon Bachmann 8d3a15d0aa
Merge pull request #23 from kamaal111/fix/crash-in-animation-on-background
Fix crash when app goes to background before animation is complete
2022-05-14 15:09:31 +02:00
Simon Bachmann 860ef2cb88
Merge pull request #25 from MarcoEidinger/README
docs: ✏️ improve code snippets in README for easy copying
2022-05-14 15:06:27 +02:00
Simon Bachmann f062bdef3f
Merge pull request #24 from twhitt14/master
One last confetti physics adjustment
2022-05-14 15:05:36 +02:00
Marco Eidinger 8451defe73 docs: ✏️ improve code snippets in README for easy copying 2022-05-02 02:59:43 -07:00
Trevor b507d48d68
One last confetti physics adjustment
This animation curve & timing results in less skipped frames from confetti initially moving too fast.
2022-04-26 13:53:25 -06:00
Kamaal Farah 4458eb45da Fix crash when app goes to background before animation is complete 2022-04-26 20:04:06 +02:00
Simon Bachmann 7b86d9f838
Merge pull request #13 from cs4alhaider/master
Added View extension to make it easier to implement it in any view
2022-04-16 20:46:45 +02:00
Simon 5b6caf2d10 package path added 2022-04-16 20:43:08 +02:00
Simon 0791408495 Merge branch 'master' of https://github.com/simibac/ConfettiSwiftUI into cs4alhaider/master 2022-04-16 20:25:05 +02:00
Simon b0ae15a2d9 typo fix 2022-04-16 20:22:38 +02:00
Simon Bachmann 726499f9e5
Merge pull request #11 from elfanek/master
Fix typo `firtAppear` to `firstAppear`
2022-04-16 20:19:58 +02:00
Simon Bachmann 2ea81e9fad
Merge branch 'master' into master 2022-04-16 20:19:41 +02:00
Simon Bachmann 9a19a70676
Merge pull request #16 from stephenmdixon/master
Fix typo of word 'couter' to 'counter'
2022-04-16 20:18:05 +02:00
Simon Bachmann c0cf2e286b
Merge pull request #14 from tunabelly/master
Added new case, sfSymbol to use an image from SF Symbols as an option.
2022-04-16 20:17:32 +02:00
Stephen Dixon 316598db8d Fix typo of word 'couter' to 'counter' 2022-03-29 15:51:11 +01:00
Matt Austin a4a5a539a1 Added new case, sfSymbol to use an image from SF Symbols as an option. 2022-03-25 16:02:38 -04:00
Abdullah Alhaider 9a71edc711 Updated with the new way 2022-03-24 23:50:26 +03:00
Abdullah Alhaider 712d8bc157 Added View extension to make it easier to implement it in any view 2022-03-24 23:47:00 +03:00
Simon Bachmann 97fa443e5c
Merge pull request #12 from twhitt14/master
Better Physics Simulation Option
2022-03-13 23:25:50 +01:00
Simon c9ecbe03a3 added new physics as only option by removing old physics 2022-03-13 23:22:37 +01:00
Omar Elfanek d718f4ee18
Fix typo `firtAppear` to `firstAppear` 2022-03-03 16:52:05 -08:00
7 changed files with 118 additions and 30 deletions

View File

@ -26,9 +26,12 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "ConfettiSwiftUI",
dependencies: []),
dependencies: [],
path: "Sources"),
.testTarget(
name: "ConfettiSwiftUITests",
dependencies: ["ConfettiSwiftUI"]),
dependencies: ["ConfettiSwiftUI"],
path: "Tests"),
]
)

View File

@ -79,19 +79,21 @@ If you prefer not to use any of dependency managers, you can integrate `Confetti
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).
```swift
import SwiftUI
import ConfettiSwiftUI
import SwiftUI
struct ContentView: View {
@State var counter:Int = 0
@State private var counter: Int = 0
var body: some View {
ZStack{
Text("🎉").font(.system(size: 50)).onTapGesture(){counter += 1}
ConfettiCannon(counter: $counter)
Button("🎉") {
counter += 1
}
.confettiCannon(counter: $counter)
}
}
```
### Parameters
@ -115,11 +117,11 @@ struct ContentView: View {
### Configurator Application With Live Preview
You can use the configurator app in [demo project here](https://github.com/simibac/ConfettiSwiftUIDemo) to make your desired animation or get inspired by one of the many examples.
<p align="center">
<img src="./Gifs/configurator.png" width="150" />
<img src="./Gifs/examples.png" width="150" />
</p>
<img src="./Gifs/configurator.png" width="150" />
<img src="./Gifs/examples.png" width="150" />
</p>
### Examples
@ -130,7 +132,7 @@ You can use the configurator app in [demo project here](https://github.com/simib
</p>
```swift
ConfettiCannon(counter: $counter2, colors: [.red, .black], confettiSize: 20)
.confettiCannon(counter: $counter, colors: [.red, .black], confettiSize: 20)
```
#### Repeat Configuration
@ -140,7 +142,7 @@ ConfettiCannon(counter: $counter2, colors: [.red, .black], confettiSize: 20)
</p>
```swift
ConfettiCannon(counter: $counter3, repetitions: 3, repetitionInterval: 0.7)
.confettiCannon(counter: $counter, repetitions: 3, repetitionInterval: 0.7)
```
#### Firework Configuration
@ -150,7 +152,7 @@ ConfettiCannon(counter: $counter3, repetitions: 3, repetitionInterval: 0.7)
</p>
```swift
ConfettiCannon(counter: $counter4, num: 50, openingAngle: Angle(degrees: 0), closingAngle: Angle(degrees: 360), radius: 200)
.confettiCannon(counter: $counter, num: 50, openingAngle: Angle(degrees: 0), closingAngle: Angle(degrees: 360), radius: 200)
```
#### Emoji Configuration
@ -160,7 +162,7 @@ ConfettiCannon(counter: $counter4, num: 50, openingAngle: Angle(degrees: 0), clo
</p>
```swift
ConfettiCannon(counter: $counter5, confettis: [.text("❤️"), .text("💙"), .text("💚"), .text("🧡")])
.confettiCannon(counter: $counter, confettis: [.text("❤️"), .text("💙"), .text("💚"), .text("🧡")])
```
#### Endless Configuration
@ -170,7 +172,7 @@ ConfettiCannon(counter: $counter5, confettis: [.text("❤️"), .text("💙"), .
</p>
```swift
ConfettiCannon(counter: $counter6, num:1, confettis: [.text("💩")], confettiSize: 20, repetitions: 100, repetitionInterval: 0.1)
.confettiCannon(counter: $counter, num:1, confettis: [.text("💩")], confettiSize: 20, repetitions: 100, repetitionInterval: 0.1)
```
#### Make-it-Rain Configuration
@ -180,7 +182,7 @@ ConfettiCannon(counter: $counter6, num:1, confettis: [.text("💩")], confettiSi
</p>
```swift
ConfettiCannon(counter: $counter7, num:1, confettis: [.text("💵"), .text("💶"), .text("💷"), .text("💴")], confettiSize: 30, repetitions: 50, repetitionInterval: 0.1)
.confettiCannon(counter: $counter, num:1, confettis: [.text("💵"), .text("💶"), .text("💷"), .text("💴")], confettiSize: 30, repetitions: 50, repetitionInterval: 0.1)
```
## 👨‍💻 Contributors

View File

@ -19,6 +19,7 @@ public enum ConfettiType:CaseIterable, Hashable {
case shape(Shape)
case text(String)
case sfSymbol(symbolName: String)
public var view:AnyView{
switch self {
@ -32,6 +33,8 @@ public enum ConfettiType:CaseIterable, Hashable {
return AnyView(RoundedCross())
case let .text(text):
return AnyView(Text(text))
case .sfSymbol(let symbolName):
return AnyView(Image(systemName: symbolName))
default:
return AnyView(Circle())
}
@ -48,8 +51,8 @@ public struct ConfettiCannon: View {
@StateObject private var confettiConfig:ConfettiConfig
@State var animate:[Bool] = []
@State var finishedAnimationCouter = 0
@State var firtAppear = false
@State var finishedAnimationCounter = 0
@State var firstAppear = false
@State var error = ""
/// renders configurable confetti animaiton
@ -112,22 +115,22 @@ public struct ConfettiCannon: View {
public var body: some View {
ZStack{
ForEach(finishedAnimationCouter..<animate.count, id:\.self){ i in
ForEach(finishedAnimationCounter..<animate.count, id:\.self){ i in
ConfettiContainer(
finishedAnimationCouter: $finishedAnimationCouter,
finishedAnimationCounter: $finishedAnimationCounter,
confettiConfig: confettiConfig
)
}
}
.onAppear(){
firtAppear = true
firstAppear = true
}
.onChange(of: counter){value in
if firtAppear{
if firstAppear{
for i in 0...confettiConfig.repetitions{
DispatchQueue.main.asyncAfter(deadline: .now() + confettiConfig.repetitionInterval * Double(i)) {
animate.append(false)
if(value < animate.count){
if(value > 0 && value < animate.count){
animate[value-1].toggle()
}
}
@ -139,7 +142,7 @@ public struct ConfettiCannon: View {
@available(iOS 14.0, macOS 11.0, watchOS 7, tvOS 14.0, *)
struct ConfettiContainer: View {
@Binding var finishedAnimationCouter:Int
@Binding var finishedAnimationCounter:Int
@StateObject var confettiConfig:ConfettiConfig
@State var firstAppear = true
@ -152,7 +155,7 @@ struct ConfettiContainer: View {
.onAppear(){
if firstAppear{
DispatchQueue.main.asyncAfter(deadline: .now() + confettiConfig.animationDuration) {
self.finishedAnimationCouter += 1
self.finishedAnimationCounter += 1
}
firstAppear = false
}
@ -188,7 +191,7 @@ struct ConfettiView: View{
}
func getAnimation() -> Animation {
return Animation.timingCurve(0, 1, 0, 1, duration: getAnimationDuration())
return Animation.timingCurve(0.1, 0.8, 0, 1, duration: getAnimationDuration())
}
func getDistance() -> CGFloat {
@ -196,7 +199,7 @@ struct ConfettiView: View{
}
func getDelayBeforeRainAnimation() -> TimeInterval {
confettiConfig.explosionAnimationDuration * 0.1
confettiConfig.explosionAnimationDuration * 0.1
}
var body: some View{
@ -279,7 +282,7 @@ class ConfettiConfig: ObservableObject {
self.radius = radius
self.repetitions = repetitions
self.repetitionInterval = repetitionInterval
self.explosionAnimationDuration = Double(radius / 1400)
self.explosionAnimationDuration = Double(radius / 1300)
self.rainAnimationDuration = Double((rainHeight + radius) / 200)
}

View File

@ -0,0 +1,80 @@
//
// View+ConfettiCannon.swift
//
//
// Created by Abdullah Alhaider on 24/03/2022.
//
import SwiftUI
public extension View {
/// renders configurable confetti animaiton
///
/// - Usage:
///
/// ```
/// import SwiftUI
///
/// struct ContentView: View {
///
/// @State private var counter: Int = 0
///
/// var body: some View {
/// Button("Wow") {
/// counter += 1
/// }
/// .confettiCannon(counter: $counter)
/// }
/// }
/// ```
///
/// - Parameters:
/// - counter: on any change of this variable the animation is run
/// - num: amount of confettis
/// - colors: list of colors that is applied to the default shapes
/// - confettiSize: size that confettis and emojis are scaled to
/// - rainHeight: vertical distance that confettis pass
/// - fadesOut: reduce opacity towards the end of the animation
/// - opacity: maximum opacity that is reached during the animation
/// - openingAngle: boundary that defines the opening angle in degrees
/// - closingAngle: boundary that defines the closing angle in degrees
/// - radius: explosion radius
/// - repetitions: number of repetitions of the explosion
/// - repetitionInterval: duration between the repetitions
///
@ViewBuilder func confettiCannon(
counter: Binding<Int>,
num: Int = 20,
confettis: [ConfettiType] = ConfettiType.allCases,
colors: [Color] = [.blue, .red, .green, .yellow, .pink, .purple, .orange],
confettiSize: CGFloat = 10.0,
rainHeight: CGFloat = 600.0,
fadesOut: Bool = true,
opacity: Double = 1.0,
openingAngle: Angle = .degrees(60),
closingAngle: Angle = .degrees(120),
radius: CGFloat = 300,
repetitions: Int = 0,
repetitionInterval: Double = 1.0
) -> some View {
ZStack {
self
ConfettiCannon(
counter: counter,
num: num,
confettis: confettis,
colors: colors,
confettiSize: confettiSize,
rainHeight: rainHeight,
fadesOut: fadesOut,
opacity: opacity,
openingAngle: openingAngle,
closingAngle: closingAngle,
radius: radius,
repetitions: repetitions,
repetitionInterval: repetitionInterval
)
}
}
}