Stop dispatch_after

后端 未结 5 768
迷失自我
迷失自我 2020-12-24 03:36

I use an animation for specify a tip to help the interaction with delay using these:

 let delay = 1.8 * Double(NSEC_PER_SEC)
    let time = dispatch_time(DIS         


        
5条回答
  •  半阙折子戏
    2020-12-24 03:58

    iOS 8 and OS X Yosemite introduced dispatch_block_cancel that allow you to cancel them before they start executing

    You declare one variable in class as follows:

    var block: dispatch_block_t?
    

    Init block variable and provide it in dispatch_after:

    block = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS) {
      print("I executed")
    }
    let time: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * NSEC_PER_SEC))
    dispatch_after(time, dispatch_get_main_queue(), block!)
    

    After that you can cancel it as follows:

    dispatch_block_cancel(block!)
    

提交回复
热议问题