stoping a confetti animation in swift

半世苍凉 提交于 2020-07-16 06:35:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!