如何在 Swift 中创建延迟? [英] How to create a delay in Swift?

查看:47
本文介绍了如何在 Swift 中创建延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某个时间点暂停我的应用.换句话说,我希望我的应用程序执行代码,但在某个时刻暂停 4 秒,然后继续执行其余代码.我该怎么做?

I want to pause my app at a certain in point. In other words, I want my app to execute the code, but then at a certain point, pause for 4 seconds, and then continue on with the rest of the code. How can I do this?

我正在使用 Swift.

I am using Swift.

推荐答案

考虑使用 NSTimer 或调度计时器,而不是睡眠(如果从 UI 线程调用会锁定您的程序).

Instead of a sleep, which will lock up your program if called from the UI thread, consider using NSTimer or a dispatch timer.

但是,如果你真的需要在当前线程中延迟:

But, if you really need a delay in the current thread:

do {
    sleep(4)
}

这使用 UNIX 中的 sleep 函数.

This uses the sleep function from UNIX.

这篇关于如何在 Swift 中创建延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆