【问题标题】:Execute action of code after certain amount of time in swift [duplicate]在一定时间后快速执行代码操作[重复]
【发布时间】:2016-10-28 07:21:22
【问题描述】:

我希望一个动作在 120 秒后运行,但是为了让其余代码继续运行而不会中断,有没有办法快速做到这一点?例如

var timer = 0
if timer == 120{
print("time up")
}
//But This code still needs to be able to be run
if buttonPressed == true{
 print("pressed")
}

【问题讨论】:

标签: ios xcode swift


【解决方案1】:

https://stackoverflow.com/a/28821805/6496271

让 triggerTime = (Int64(NSEC_PER_SEC) * 10) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in self.functionToCall() })

10 秒后调用 self.functionToCall()

让它达到 120 秒:

let triggerTime = (Int64(NSEC_PER_SEC) * 120)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in
    self.functionToCall()
})

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2021-03-28
    • 1970-01-01
    • 2015-03-27
    • 2018-12-10
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多