Ios 如何等待给定的时间运行操作
我想知道在使控制台计数给定时间后如何运行操作。例如,在3秒钟后打印“hello”。Ios 如何等待给定的时间运行操作,ios,swift,Ios,Swift,我想知道在使控制台计数给定时间后如何运行操作。例如,在3秒钟后打印“hello”。 谢谢 您需要使用NSTimer类 用法示例: var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("sayHello"), userInfo: nil, repeats: true) 在你们班的其他地方 func sayHello() { println("Hello") } 您可
谢谢 您需要使用
NSTimer
类
用法示例:
var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("sayHello"), userInfo: nil, repeats: true)
在你们班的其他地方
func sayHello() {
println("Hello")
}
您可以通过以下方式创建延迟!! 您可以使用
asynAfter
DispatchQueue.main.asyncAfter(deadline: .now() + <secondsToDelay>) {
// Put your code here that you want to execute after delay
}
当然,你也可以像伍德斯托克的回答一样使用
定时器。你考虑过并尝试过什么?谢谢你的帮助:)@ManewL很乐意帮忙。
perform(#selector(delayedMethod), with: nil, afterDelay: <secondsToDelay>)
@objc func delayedMethod() {
print("My delayed method call")
}