码迷,mamicode.com
首页 > 编程语言 > 详细

swift dispatch_after

时间:2016-05-14 20:13:06      阅读:384      评论:0      收藏:0      [点我收藏+]

标签:

typealias Task = (cancel : Bool) -> ()
    func delay(time:NSTimeInterval, task:()->()) ->  Task? {
        func dispatch_later(block:()->()) {
            dispatch_after(
                dispatch_time(
                    DISPATCH_TIME_NOW,
                    Int64(time * Double(NSEC_PER_SEC))),
                dispatch_get_main_queue(),
                block)
        }
        var closure: dispatch_block_t? = task
        var result: Task?
        let delayedClosure: Task = {
            cancel in
            if let internalClosure = closure {
                if (cancel == false) {
                    dispatch_async(dispatch_get_main_queue(), internalClosure);
                }
            }
            closure = nil
            result = nil
        }
        result = delayedClosure
        dispatch_later {
            if let delayedClosure = result {
                delayedClosure(cancel: false)
            }
        }
        return result;
    }
    func cancel(task:Task?) {
        task?(cancel: true)
    }

 

swift dispatch_after

标签:

原文地址:http://www.cnblogs.com/newBlash/p/5493186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!