dispatch_after - GCD in Swift?

前端 未结 25 2871
心在旅途
心在旅途 2020-11-21 06:07

I\'ve gone through the iBook from Apple, and couldn\'t find any definition of it:

Can someone explain the structure of dispatch_after?

d         


        
25条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 06:24

    1) Add this method as a part of UIViewController Extension.

    extension UIViewController{
    func runAfterDelay(delay: NSTimeInterval, block: dispatch_block_t) {
            let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
            dispatch_after(time, dispatch_get_main_queue(), block)
        }
    }
    

    Call this method on VC:

        self.runAfterDelay(5.0, block: {
         //Add code to this block
            print("run After Delay Success")
        })
    

    2)

    performSelector("yourMethod Name", withObject: nil, afterDelay: 1)
    

    3)

    override func viewWillAppear(animated: Bool) {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2), dispatch_get_main_queue(), { () -> () in
        //Code Here
    })
    

    //Compact Form

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2), dispatch_get_main_queue()) {
        //Code here
     }
    }
    

提交回复
热议问题