我,我尝试使用Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { (Timer) in
,但它不符合我的要求。这是我的想法:
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")
}
答案 0 :(得分:0)
试试这个:
在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()
}
}
希望它有所帮助!
答案 1 :(得分:-1)
如果计时器= 2秒&#34;它并不清楚你的意思是&#34;
此代码是在重复运行的函数中,还是在块内运行的代码用于计时器?
代码可能看起来像这样
df2 <- df %>% mutate(clock = chron(times = clock)) %>% mutate(rolex = chron(times = rolex))
}