我想在我的代码中庆祝某个事件。所以我从 GitHub ( https://github.com/sudeepag/SAConfettiView ) 上快速地覆盖了五彩纸屑,并试图在我的项目中使用它。基本上,如果某个事件发生,它会启动一个 NSTimer。我还定义了一个 showAlert
和 dismissAlert
用于在 UIAlertView 上弹出无按钮消息以说出 Congrats!
.当计时器计时confettitime
(从 0 开始)第一次,它开始覆盖五彩纸屑(成功)。然后,在第三个滴答声中,它激活 showAlert
.消息弹出。它使用 dismissAlert
成功解散在计时器的第七个滴答声中。但我无法让覆盖层隐藏/五彩纸屑消失。根据该 GitHub 项目中的说明,我调用 confettiView.stopConfetti()
让它停下来!但它只是继续显示五彩纸屑,卡住我的用户界面。我尝试删除通知(无效),显着改变五彩纸屑代码(无效),但我找不到隐藏覆盖五彩纸屑 View 的方法。
我关注了Manual Installation
GitHub 项目中的说明,添加图像 Assets 和 SAConfettiView.swift
到我的 Swift XCode 项目。我不得不更改几个代码位才能启动,因为它们崩溃了(就像这条评论说: https://github.com/sudeepag/SAConfettiView/pull/6 ),所以也许这会影响它?
这是 SAConfettiView 的代码,它是那个 GitHub 项目的产品:
import UIKit
import QuartzCore
public class SAConfettiView: UIView {
public enum ConfettiType {
case Confetti
case Triangle
case Star
case Diamond
case Image(UIImage)
}
var emitter: CAEmitterLayer!
public var colors: [UIColor]!
public var intensity: Float!
public var type: ConfettiType!
private var active :Bool!
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup() {
colors = [UIColor(red:0.95, green:0.40, blue:0.27, alpha:1.0),
UIColor(red:1.00, green:0.78, blue:0.36, alpha:1.0),
UIColor(red:0.48, green:0.78, blue:0.64, alpha:1.0),
UIColor(red:0.30, green:0.76, blue:0.85, alpha:1.0),
UIColor(red:0.58, green:0.39, blue:0.55, alpha:1.0)]
intensity = 0.5
type = .Confetti
active = false
}
public func startConfetti() {
emitter = CAEmitterLayer()
emitter.emitterPosition = CGPoint(x: frame.size.width / 2.0, y: 0)
emitter.emitterShape = kCAEmitterLayerLine
emitter.emitterSize = CGSize(width: frame.size.width, height: 1)
var cells = [CAEmitterCell]()
for color in colors {
cells.append(confettiWithColor(color))
}
emitter.emitterCells = cells
layer.addSublayer(emitter)
active = true
}
public func stopConfetti() {
emitter?.birthRate = 0
active = false
}
func imageForType(type: ConfettiType) -> UIImage? {
print(type)
var fileName: String!
switch type {
case .Confetti:
fileName = "Confetti"
case .Triangle:
fileName = "triangle"
case .Star:
fileName = "star"
case .Diamond:
fileName = "diamond"
case let .Image(customImage):
return customImage
}
print(type)
return UIImage(imageLiteral: "\(type)")
}
func confettiWithColor(color: UIColor) -> CAEmitterCell {
let confetti = CAEmitterCell()
confetti.birthRate = 6.0 * intensity
confetti.lifetime = 14.0 * intensity
confetti.lifetimeRange = 0
confetti.color = color.CGColor
confetti.velocity = CGFloat(350.0 * intensity)
confetti.velocityRange = CGFloat(80.0 * intensity)
confetti.emissionLongitude = CGFloat(M_PI)
confetti.emissionRange = CGFloat(M_PI_4)
confetti.spin = CGFloat(3.5 * intensity)
confetti.spinRange = CGFloat(4.0 * intensity)
confetti.scaleRange = CGFloat(intensity)
confetti.scaleSpeed = CGFloat(-0.1 * intensity)
confetti.contents = imageForType(type)!.CGImage
return confetti
}
public func isActive() -> Bool {
return self.active
}
}
这是我项目中的相关代码,我称之为五彩纸屑:
var confettitimer = NSTimer()
var confettitime = 0
var alreadyconfettied = false
func confetti() {
confettitime++
let confettiView = SAConfettiView(frame: self.view.bounds)
self.view.addSubview(confettiView)
if confettitime == 1 {
confettiView.type = .Confetti
confettiView.startConfetti()
alreadyconfettied = true
}
if confettitime == 3 {
showAlert2()
}
if confettitime == 7 {
dismissAlert2()
}
if confettitime == 10 {
confettiView.stopConfetti()
lp.confettitimer.invalidate()
lp.confettitime = 0
}
}
if event == true && alreadyconfettied == false {
confettitimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "confetti", userInfo: nil, repeats: true)
alreadyconfettied = true
}
很抱歉所有的代码!如果您能告诉我,那将非常有帮助我怎样才能让这个五彩纸屑覆盖停止!
更新1(仍未解决):
所以我需要让 ConfettiView 创建发生在与
.startConfetti()
相同的函数中。和 .stopConfetti()
!没有办法用计时器来做到这一点,因为当两者都在同一个函数中时,计时器不能调用一个函数(见这里:https://stackoverflow.com/a/25839597/5700898)。现在,当某个事件发生时,它会调用 confetti() 函数... func confetti() { // called once and only once when the event is triggered.
let confettiView = SAConfettiView(frame: self.view.bounds)
self.view.addSubview(confettiView)
var confettitimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "confettisub", userInfo: nil, repeats: true)
func confettisubfunction() {
lp.confettitime++
print(lp.confettitime)
if confettitime == 1 {
confettiView.type = .Confetti
confettiView.startConfetti()
alreadyconfettied = true
}
if confettitime == 3 {
showAlert2()
}
if confettitime == 7 {
dismissAlert2()
}
if confettitime == 10 {
confettiView.stopConfetti()
confettitimer.invalidate()
confettitime = 0
}
}
}
但这会引发我上面所说的灾难性错误。基本上,这种设置是不可能的,因为 View 的声明不能和我用
.startConfetti()
调用它的函数在同一个函数中。 ,并且它不能是全局变量/声明:- 如果我这样做
let confettiView = SAConfettiView(frame: self.view.bounds)
self.view.addSubview(confettiView)
就在 class ViewController: UIViewController {
之前,然后它不知道 self 是什么,并给出错误“表达式不允许在顶层。”- 如果我在 View Controller 的声明中执行相同的代码来定义我的 Confetti UIView,它会显示“预期声明”。
那么,当某个事件发生时,我该如何解决这个问题并让五彩纸屑下雨,以及如何在计时器的帮助下停止所说的五彩纸屑(或者真的任何可以工作的东西??)
提前感谢您的帮助!
更新2(几乎解决):
在我的全局范围内,我基本上是这样做的,我调用
initconfetti()
每次庆祝事件一次。这让我只有一个五彩纸屑 View ,我称之为 startConfetti()
和 stopConfetti()
上,让我真的再一次停止五彩纸屑!var confettiView = SAConfettiView()
func initconfetti() {
confettiView = SAConfettiView(frame: self.view.bounds)
self.view.addSubview(confettiView)
}
现在我遇到了一个问题:我无法重新点击原始 View (我的 View Controller )!我如何消除五彩纸屑的观点?也许我把边界设为 0,0?我必须为此努力(我仍然想要一些建议)。
当五彩纸屑结束时,我正在调用这一系列命令(我真的不知道它们都是什么意思):
confettiView.removeFromSuperview()
confettiView = SAConfettiView(frame: CGRect(
origin: CGPoint(x: 0, y: 0),
size: CGSize(width: 0, height: 0)))
view.becomeFirstResponder()
self.view.becomeFirstResponder()
但是我的 View Controller 仍然没有响应任何地方的点击,什么都没有发生!该怎么办?有什么功能可以让我的旧 View 再次出现在前台吗?
谢谢!
最佳答案
每次输入您的confetti
函数你创建一个新的SAConfettiView
并将其添加到 Controller 的 View 中。我没有关注所有 if
声明,但我认为可以肯定地说您调用的 View stopConfetti
on 与您用于 startConfetti
的 View 不同.
我建议在定时器例程之外创建 View ,并且只通过定时器来控制它。
关于ios - 无法让 UIView 的子类停止显示 [swift],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35960553/