Wait/Delay 在 Xcode (Swift)
Wait/Delay in Xcode (Swift)
如何在 Xcode 中添加延迟?
self.label1.alpha = 1.0
//delay
self.label1.alpha = 0.0
我想让它等待大约 2 秒。我读过 time_dispatch 和导入达尔文库,但我没能让它工作。那么有人可以逐步正确解释吗?
对于这个使用块可能更好:
self.label1.alpha = 1.0;
UIView animateWithDuration:2.0 animations:^(void) {
self.label1.alpha = 0.0;
}];
您只需编写以下代码:
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' 是您要等待的秒数
此致
这是另一个可行的选项 -
import Darwin
sleep(2)
然后,你可以使用sleep函数,这个函数以秒数为参数。
对于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
}
这对我有用。
参考:
如何在 Xcode 中添加延迟?
self.label1.alpha = 1.0
//delay
self.label1.alpha = 0.0
我想让它等待大约 2 秒。我读过 time_dispatch 和导入达尔文库,但我没能让它工作。那么有人可以逐步正确解释吗?
对于这个使用块可能更好:
self.label1.alpha = 1.0;
UIView animateWithDuration:2.0 animations:^(void) {
self.label1.alpha = 0.0;
}];
您只需编写以下代码:
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' 是您要等待的秒数
此致
这是另一个可行的选项 -
import Darwin
sleep(2)
然后,你可以使用sleep函数,这个函数以秒数为参数。
对于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
}
这对我有用。
参考: