在斯威夫特的GCD后派人去吗?我经历了iBook来自苹果,却找不到它的任何定义:有人能解释一下…的结构吗?dispatch_after?dispatch_after(<#when: dispatch_time_t#>, <#queue: dispatch_queue_t?#>, <#block: dispatch_block_t?#>)
4 回答
侃侃尔雅
TA贡献1801条经验 获得超15个赞
dispatch_after
func delay(delay:Double, closure:()->()) { dispatch_after( dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)) ), dispatch_get_main_queue(), closure)}
delay(0.4) { // do stuff}
更新SWIFT 3,Xcode 8种子6
func delay(_ delay:Double, closure:@escaping ()->()) { let when = DispatchTime.now() + delay DispatchQueue.main.asyncAfter(deadline: when, execute: closure)}
呼唤远方
TA贡献1856条经验 获得超11个赞
let delay = 4.5 * Double(NSEC_PER_SEC)let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))dispatch_after(time, dispatch_get_main_queue(), block)
=SWIFT 3的更新=
DispatchQueue.main.asyncAfter(deadline: .now() + 4.5) { // ...}
千巷猫影
TA贡献1829条经验 获得超7个赞
typealias dispatch_cancelable_closure = (cancel : Bool) -> Voidfunc delay(time:NSTimeInterval, closure:()->Void) -> dispatch_cancelable_closure? { func dispatch_later(clsr:()->Void) { dispatch_after( dispatch_time( DISPATCH_TIME_NOW, Int64(time * Double(NSEC_PER_SEC)) ), dispatch_get_main_queue(), clsr) } var closure:dispatch_block_t? = closure var cancelableClosure:dispatch_cancelable_closure? let delayedClosure:dispatch_cancelable_closure = { cancel in if closure != nil { if (cancel == false) { dispatch_async(dispatch_get_main_queue(), closure!); } } closure = nil cancelableClosure = nil } cancelableClosure = delayedClosure dispatch_later { if let delayedClosure = cancelableClosure { delayedClosure(cancel: false) } } return cancelableClosure;}func cancel_delay(closure:dispatch_cancelable_closure?) { if closure != nil { closure!(cancel: true) }}
let retVal = delay(2.0) { println("Later")}delay(1.0) { cancel_delay(retVal)}
- 4 回答
- 0 关注
- 502 浏览
添加回答
举报
0/150
提交
取消