Xcode中的等待/延迟(Swift)

时间:2015-08-31 17:22:38

标签: xcode swift delay

如何在Xcode中添加延迟?

QWidget

我想让它等待大约2秒钟。我已经阅读了关于time_dispatch和导入darwin库的内容,但我还没有能够使它工作。那么有人可以一步一步地解释它吗?

4 个答案:

答案 0 :(得分:7)

您只需编写此代码:

self.label1.alpha = 1.0    

let delay = 2 * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue()) {
    // After 2 seconds this line will be executed            
    self.label1.alpha = 0.0
}

' 2'是你想要等待的秒数

此致

答案 1 :(得分:6)

这是另一种有效的选择 -

i = 734146560 @@@ On connect callback 0@@@

然后,您可以使用sleep函数,该函数需要几秒钟作为参数。

答案 2 :(得分:3)

最好使用这个块:

self.label1.alpha = 1.0;

UIView animateWithDuration:2.0 animations:^(void) {
    self.label1.alpha = 0.0;
}];

答案 3 :(得分:0)

对于Swift 5:

self.label1.alpha = 1.0 

let delay : Double = 2.0 //delay time in seconds
let time = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline:time){
    // After 2 seconds this line will be executed
    self.label1.alpha = 0.0
}

这对我有用。

引用:Delay using DispatchTime.now() + float?