swift延时执行

更新时间:2023-06-11 16:09:57 阅读: 评论:0

swift延时执⾏
1.延时执⾏
//延时1秒执⾏
nets
let time: TimeInterval = 1.0
DispatchQueue.main.asyncAfter(deadline: w() + time) {
//code
print("1 秒后输出")
}
2.对延时执⾏喵神进⾏了较好的封装,这⾥我也贴出来了,供⼤家学习
matterhornimport Foundation
typealias Task = (_ cancel : Bool) -> Void
func delay(_ time: TimeInterval, task: @escaping ()->()) ->  Task? {
func dispatch_later(block: @escaping ()->()) {
let t = w() + timemotioncontrol
DispatchQueue.main.asyncAfter(deadline: t, execute: block)
}爱你在心口难开英文版
var closure: (()->Void)? = task
var result: Task?
let delayedClosure: Task = {双学位英文
马尔克斯的名言cancel in
if let internalClosure = closure {
if (cancel == fal) {
DispatchQueue.main.async(execute: internalClosure)
}
}
behavesclosure = nil
result = nil
customerrvice}
result = delayedClosure
dispatch_later {
if let delayedClosure = result {
delayedClosure(fal)
}
}
return result
}
func cancel(_ task: Task?) {
task?(true)
日语能力考
}
/*****使⽤*****/
//调⽤
delay(2) { print("2 秒后输出") }
//取消
let task = delay(5) { print("拨打 110") }
/
/ 仔细想⼀想..
// 还是取消为妙..
cancel(task)
3.OC版本
//延时
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{          //do something
});
谦冲

本文发布于:2023-06-11 16:09:57,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/930668.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图