迅速停止五彩纸屑动画 [英] stoping a confetti animation in swift

查看:116
本文介绍了迅速停止五彩纸屑动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个跟踪目标进度的应用程序.一旦目标完成并达到100%,就会显示五彩纸屑的动画,但不会停止.一定时间后如何停止五彩纸屑动画?我会使用计时器吗?

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)

这是保存displayConfetti()函数的函数.

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
    }

showTrophy()的先前功能最终在此if语句中显示.

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.

推荐答案

func displayConfetti() -> Emitter {
    ...
    return emitter
}

func showTrophy() {
    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
        ...
        let emitter = self.displayConfetti()
        DispatchQueue.main.asyncAfter(deadline: someTime) {
            emitter.removeFromSuperLayer()
        }
    }
}

这篇关于迅速停止五彩纸屑动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆