ios 如何在 Swift 3 中编程延迟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38031137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to program a delay in Swift 3
提问by owlswipe
In earlier versions of Swift, one could create a delay with the following code:
在早期版本的 Swift 中,可以使用以下代码创建延迟:
let time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 4 * Int64(NSEC_PER_SEC))
dispatch_after(time, dispatch_get_main_queue()) {
//put your code which should be executed with a delay here
}
But now, in Swift 3, Xcode automatically changes 6 different things but then the following error appears: "Cannot convert DispatchTime.now
to expected value dispatch_time_t
aka UInt64
."
但是现在,在 Swift 3 中,Xcode 会自动更改 6 项不同的内容,但随后会出现以下错误:“无法转换DispatchTime.now
为预期值dispatch_time_t
aka ” UInt64
。
How can one create a delay before running a sequence of code in Swift 3?
如何在 Swift 3 中运行一系列代码之前创建延迟?
回答by owlswipe
After a lot of research, I finally figured this one out.
经过大量研究,我终于弄清楚了这一点。
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { // Change `2.0` to the desired number of seconds.
// Code you want to be delayed
}
This creates the desired "wait" effect in Swift 3 and Swift 4.
这会在 Swift 3 和 Swift 4 中创建所需的“等待”效果。
Inspired by a part of this answer.
灵感来自这个答案的一部分。
回答by Victor Do
I like one-line notation for GCD, it's more elegant:
我喜欢 GCD 的单行符号,它更优雅:
DispatchQueue.main.asyncAfter(deadline: .now() + 42.0) {
// do stuff 42 seconds later
}
Also, in iOS 10 we have new Timer methods, e.g. block initializer:
此外,在 iOS 10 中,我们有新的 Timer 方法,例如块初始值设定项:
(so delayed action may be canceled)
(所以延迟的行动可能会被取消)
let timer = Timer.scheduledTimer(withTimeInterval: 42.0, repeats: false) { (timer) in
// do stuff 42 seconds later
}
Btw, keep in mind: by default, timer is added to the default run loop mode. It means timer may be frozen when the user is interacting with the UI of your app (for example, when scrolling a UIScrollView) You can solve this issue by adding the timer to the specific run loop mode:
顺便说一句,请记住:默认情况下,计时器被添加到默认的运行循环模式中。这意味着当用户与您的应用程序的 UI 交互时计时器可能会被冻结(例如,滚动 UIScrollView 时)您可以通过将计时器添加到特定运行循环模式来解决此问题:
RunLoop.current.add(timer, forMode: .common)
At this blog postyou can find more details.
在此博客文章中,您可以找到更多详细信息。
回答by Vakas
Try the following function implemented in Swift 3.0 and above
试试下面在Swift 3.0 及以上版本中实现的函数
func delayWithSeconds(_ seconds: Double, completion: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
completion()
}
}
Usage
用法
delayWithSeconds(1) {
//Do something
}
回答by Anand Verma
Try the below code for delay
试试下面的代码延迟
//MARK: First Way
func delayForWork() {
delay(3.0) {
print("delay for 3.0 second")
}
}
delayForWork()
// MARK: Second Way
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// your code here delayed by 0.5 seconds
}
回答by Zohaib Brohi
One way is to use DispatchQueue.main.asyncAfter
as a lot of people have answered.
一种方法是使用,DispatchQueue.main.asyncAfter
因为很多人已经回答过。
Another way is to use perform(_:with:afterDelay:)
. More details here
另一种方法是使用perform(_:with:afterDelay:)
. 更多细节在这里
perform(#selector(delayedFunc), with: nil, afterDelay: 3)
@IBAction func delayedFunc() {
// implement code
}
回答by Pratyush Pratik
//Runs function after x seconds
//x 秒后运行函数
public static func runThisAfterDelay(seconds: Double, after: @escaping () -> Void) {
runThisAfterDelay(seconds: seconds, queue: DispatchQueue.main, after: after)
}
public static func runThisAfterDelay(seconds: Double, queue: DispatchQueue, after: @escaping () -> Void) {
let time = DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
queue.asyncAfter(deadline: time, execute: after)
}
//Use:-
//用:-
runThisAfterDelay(seconds: x){
//write your code here
}