问题
I am making an app that tracks the progress of goals. once a goal is complete, hitting 100%, an animation of confetti is displayed, but it won't stop. how would I stop the confetti animation after a certain amount of time? would I use a timer?
func displayConfetti() {
//creates purple confetti
let purpleConfetti: UIImage = UIImage(named: "purpleConfetti")!
let emitter = Emitter.get(with: purpleConfetti)
emitter.emitterPosition = CGPoint(x: view.frame.size.width / 2, y: -10)
emitter.emitterSize = CGSize(width: view.frame.size.width, height: 2)
view.layer.addSublayer(emitter)
this is the function that holds the displayConfetti() function.
func showTrophy() {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
let popUp = TrophyPopUp()
self.view.addSubview(popUp)
self.displayConfetti()
//function that stops confetti after a certain amount of time
}
the previous function of showTrophy() is finally displayed in this if statement.
if countingIncrement == 100 {
countingLabel.removeFromSuperview()
goalComplete()
showTrophy()
}
how would I solve this problem and where would I add the code? any suggestions help. thanks.
回答1:
func displayConfetti() -> Emitter {
...
return emitter
}
func showTrophy() {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
...
let emitter = self.displayConfetti()
DispatchQueue.main.asyncAfter(deadline: someTime) {
emitter.removeFromSuperLayer()
}
}
}
来源:https://stackoverflow.com/questions/61721061/stoping-a-confetti-animation-in-swift