ios - 如何在单个 UI 按钮上启动和停止 Confetti View?
问题描述
我是 Xcode 的初学者。我正在尝试制作一个在单击按钮时显示五彩纸屑的应用程序。但是,当我按下按钮时,会显示五彩纸屑,但它永远不会停止。它阻止按钮和视图。我正在使用 Cocoapod - SAConfettiView。
这段代码在 Xcode 10 上。我相信 Confetti 挡住了主视图。我使用 playConfetti 作为 IBaction 函数。
@IBAction func playConfetti(_ sender: UIButton, forEvent event: SAConfettiView) {
let confettiView = SAConfettiView(frame: self.view.bounds)
confettiView.intensity = 1
confettiView.type = .Star
confettiView.startConfetti()
view.addSubview(confettiView)
}
我必须使用 If-Else 语句吗?谢谢!
解决方案
更新的答案
要停止SAConfettiView
调用该函数,
confettiView.stopConfetti()
将您的按钮操作代码更改为以下内容:
let confettiView = SAConfettiView()
@IBAction func startClicked(_ sender: Any)
if confettiView.isActive() {
confettiView.stopConfetti()
confettiView.removeFromSuperview()
}
else{
confettiView = SAConfettiView(frame: self.view.bounds)
confettiView.intensity = 1
confettiView.type = .Star
confettiView.startConfetti()
view.addSubview(confettiView)
self.view.bringSubviewToFront(sender as! UIButton)
}
}