Skip to content

Commit 97fa443

Browse files
author
Simon Bachmann
authored
Merge pull request #12 from twhitt14/master
Better Physics Simulation Option
2 parents cab0145 + c9ecbe0 commit 97fa443

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Diff for: .swiftpm/xcode/xcshareddata/xcschemes/ConfettiSwiftUI.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1310"
3+
LastUpgradeVersion = "1320"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Diff for: Sources/ConfettiSwiftUI/ConfettiSwiftUI.swift

+26-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public struct ConfettiCannon: View {
7979
radius:CGFloat = 300,
8080
repetitions:Int = 0,
8181
repetitionInterval:Double = 1.0
82-
8382
) {
8483
self._counter = counter
8584
var shapes = [AnyView]()
@@ -179,13 +178,33 @@ struct ConfettiView: View{
179178
let spinDirections:[CGFloat] = [-1.0, 1.0]
180179
return spinDirections.randomElement()!
181180
}
181+
182+
func getRandomExplosionTimeVariation() -> CGFloat {
183+
CGFloat((0...999).randomElement()!) / 2100
184+
}
185+
186+
func getAnimationDuration() -> CGFloat {
187+
return 0.2 + confettiConfig.explosionAnimationDuration + getRandomExplosionTimeVariation()
188+
}
189+
190+
func getAnimation() -> Animation {
191+
return Animation.timingCurve(0, 1, 0, 1, duration: getAnimationDuration())
192+
}
193+
194+
func getDistance() -> CGFloat {
195+
return pow(CGFloat.random(in: 0.01...1), 2.0/7.0) * confettiConfig.radius
196+
}
197+
198+
func getDelayBeforeRainAnimation() -> TimeInterval {
199+
confettiConfig.explosionAnimationDuration * 0.1
200+
}
182201

183202
var body: some View{
184203
ConfettiAnimationView(shape:getShape(), color:getColor(), spinDirX: getSpinDirection(), spinDirZ: getSpinDirection())
185204
.offset(x: location.x, y: location.y)
186205
.opacity(opacity)
187206
.onAppear(){
188-
withAnimation(Animation.timingCurve(0.61, 1, 0.88, 1, duration: confettiConfig.explosionAnimationDuration)) {
207+
withAnimation(getAnimation()) {
189208
opacity = confettiConfig.opacity
190209

191210
let randomAngle:CGFloat
@@ -195,13 +214,13 @@ struct ConfettiView: View{
195214
randomAngle = CGFloat.random(in: CGFloat(confettiConfig.openingAngle.degrees)...CGFloat(confettiConfig.closingAngle.degrees + 360)).truncatingRemainder(dividingBy: 360)
196215
}
197216

198-
let distance = CGFloat.random(in: 0.5...1) * confettiConfig.radius
217+
let distance = getDistance()
199218

200219
location.x = distance * cos(deg2rad(randomAngle))
201220
location.y = -distance * sin(deg2rad(randomAngle))
202221
}
203222

204-
DispatchQueue.main.asyncAfter(deadline: .now() + confettiConfig.explosionAnimationDuration) {
223+
DispatchQueue.main.asyncAfter(deadline: .now() + getDelayBeforeRainAnimation()) {
205224
withAnimation(Animation.timingCurve(0.12, 0, 0.39, 0, duration: confettiConfig.rainAnimationDuration)) {
206225
location.y += confettiConfig.rainHeight
207226
opacity = confettiConfig.fadesOut ? 0 : confettiConfig.opacity
@@ -225,9 +244,9 @@ struct ConfettiAnimationView: View {
225244

226245

227246
@State var move = false
228-
@State var xSpeed:Double = Double.random(in: 1...2)
247+
@State var xSpeed:Double = Double.random(in: 0.501...2.201)
229248

230-
@State var zSpeed = Double.random(in: 1...2)
249+
@State var zSpeed = Double.random(in: 0.501...2.201)
231250
@State var anchor = CGFloat.random(in: 0...1).rounded()
232251

233252
var body: some View {
@@ -260,7 +279,7 @@ class ConfettiConfig: ObservableObject {
260279
self.radius = radius
261280
self.repetitions = repetitions
262281
self.repetitionInterval = repetitionInterval
263-
self.explosionAnimationDuration = Double(radius / 1500)
282+
self.explosionAnimationDuration = Double(radius / 1400)
264283
self.rainAnimationDuration = Double((rainHeight + radius) / 200)
265284
}
266285

0 commit comments

Comments
 (0)