Swift if语句中的秒计数器
一、 我尝试在中使用Swift if语句中的秒计数器,swift,Swift,一、 我尝试在中使用Timer.scheduledTimer(withTimeInterval:2,repeats:false,block:{(Timer)),但它无法按我所希望的方式工作。以下是我的想法: If certatin rotation is true { start timer; if timer = 2seconds{ perform some action } if timer = 3 seconds{ perform another action } }else sto
Timer.scheduledTimer(withTimeInterval:2,repeats:false,block:{(Timer)),但它无法按我所希望的方式工作。以下是我的想法:
If certatin rotation is true {
start timer;
if timer = 2seconds{
perform some action
}
if timer = 3 seconds{
perform another action
}
}else stop timer
我的代码:
//===============================================FIRTS
if Counter == 1 {
if self.Bottle.zRotation < -0.94 && self.Bottle.zPosition > -0.98 {
self.WaterDrop.alpha = 1
Timer.scheduledTimer(withTimeInterval: 1, repeats: false, block: { (Timer) in
self.GlassWater1.alpha = 0.5
self.BottleWater6.alpha = 0.0
self.WaterDrop.setScale(CGFloat(1.2))
})
Counter = 2
print(Counter)
}else {
self.WaterDrop.alpha = 0.0
self.WaterDrop.setScale(CGFloat(1.0))
Counter = 1
// print(Counter)
}
}
//===============================================SECOND
if Counter == 2 {
if self.Bottle.zRotation < -0.94 && self.Bottle.zPosition > -0.98 {
self.WaterDrop.alpha = 1
Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { (Timer) in
self.GlassWater2.alpha = 0.5
self.BottleWater5.alpha = 0.0
self.WaterDrop.setScale(CGFloat(1.4))
})
Counter = 3
// print(Counter)
}else {
self.WaterDrop.alpha = 0.0
self.WaterDrop.setScale(CGFloat(1.0))
Counter = 1
// print(Counter)
}
}
//===============================================Third
if Counter == 3 {
if self.Bottle.zRotation < -0.94 && self.Bottle.zPosition > -0.98 {
self.WaterDrop.alpha = 1
Timer.scheduledTimer(withTimeInterval: 3, repeats: false, block: { (Timer) in
self.GlassWater3.alpha = 0.5
self.BottleWater4.alpha = 0.0
self.WaterDrop.setScale(CGFloat(1.4))
})
Counter = 4
// print(Counter)
}else {
self.WaterDrop.alpha = 0.0
self.WaterDrop.setScale(CGFloat(1.0))
Counter = 1
// print(Counter)
}
}
// print("tilred in z")
}
/==================================================================================================================================================================================================================================================================================================================
如果计数器==1{
如果self.battle.z旋转<-0.94&&self.battle.z位置>-0.98{
self.WaterDrop.alpha=1
scheduledTimer(withTimeInterval:1,repeats:false,block:{(Timer)in)
self.GlassWater1.alpha=0.5
自身瓶装水6.alpha=0.0
自身水滴设置刻度(CGFloat(1.2))
})
计数器=2
打印(计数器)
}否则{
self.WaterDrop.alpha=0.0
自身水滴设置刻度(CGFloat(1.0))
计数器=1
//打印(计数器)
}
}
//=================================================================================秒
如果计数器==2{
如果self.battle.z旋转<-0.94&&self.battle.z位置>-0.98{
self.WaterDrop.alpha=1
scheduledTimer(带时间间隔:2,重复:false,块:{(计时器)in
self.GlassWater2.alpha=0.5
自身瓶装水5.alpha=0.0
自身水滴设置刻度(CGFloat(1.4))
})
计数器=3
//打印(计数器)
}否则{
self.WaterDrop.alpha=0.0
自身水滴设置刻度(CGFloat(1.0))
计数器=1
//打印(计数器)
}
}
//第三名
如果计数器==3{
如果self.battle.z旋转<-0.94&&self.battle.z位置>-0.98{
self.WaterDrop.alpha=1
scheduledTimer(带时间间隔:3,重复:false,块:{(计时器)in
self.GlassWater3.alpha=0.5
自身瓶装水4.alpha=0.0
自身水滴设置刻度(CGFloat(1.4))
})
计数器=4
//打印(计数器)
}否则{
self.WaterDrop.alpha=0.0
自身水滴设置刻度(CGFloat(1.0))
计数器=1
//打印(计数器)
}
}
//打印(“在z中平铺”)
}
试试这个:
在viewController中获取变量time
:
var time = 0
然后这样做:
Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
time = time.advanced(by: 1)
if time == 2 {
//Perform something at 2 seconds
}else if time == 3 {
//Perform something at 3 seconds
}else {
//Stop Timer
timer.invalidate()
}
}
希望能有帮助!不清楚“如果计时器=2秒”是什么意思
这段代码是重复运行的函数中的代码,还是在计时器块中运行的代码
代码可能如下所示
//Create an instance variable to track how many times the timer has fired.
var count: Int = 0
count = 0 //Reset the count so we start at 0 every time we trigger a timer.
Timer.scheduledTimer(withTimeInterval: 1.0 repeats: true block) { timer in
self.count += 1
if count == 2 {
//your action code
} else if count == 3 {
//your other action code
} else if count >= 4 {
timer.invalidate()
}
}您必须在RunLoop
,RunLoop.current.add(putYourTimerHere,forMode:.defaultRunLoopMode)上安装新创建的计时器
@user9335240不,您不能。API scheduledTimer(将计时器隐式添加到runloop。@瓦迪安,很抱歉,感谢您的澄清,如果certatin旋转为真,它应该是这样的?如果{runloop.current.add(timer.scheduledTimer(withTimeInterval:2,repeats:false,block:{(timer)in code_HERE})runloop.current.add(timer.scheduledTimer)(withTimeInterval:3,repeats:false,block:{(Timer)in Other_Code_Here}}
?它不符合我的要求
-请添加Swift代码而不是伪代码,如果没有实际代码,我们很难判断出哪里出了问题。你抢先了我一步!(你在我写我的时候发布了你的答案。)如果时间==2,你的代码加上额外的ifs就完美了。谢谢你!@Paweł捷克快乐编码!!