停止dispatch_after [英] Stop dispatch_after

查看:417
本文介绍了停止dispatch_after的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用动画来指定提示,以帮助延迟交互,方法如下:

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(DISPATCH_TIME_NOW, Int64(delay))
    dispatch_after(time, dispatch_get_main_queue()) {
        //call the method which have the steps after delay.

        self.rain.alpha = 0

        UIView.animateWithDuration(5, animations: {

            self.rain.alpha = 1

        })

        self.tip.startAnimating()

    }

但是,如果在动画开始之前用户触摸屏幕,我需要停止此延迟过程。

But, I need to stop this delay process if, before animation start, user touch the screen.

推荐答案

iOS 8和OS X Yosemite引入了 dispatch_block_cancel ,可让您在开始执行之前取消它们

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?

初始化 block 变量并将其提供在 dispatch_after

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!)

这篇关于停止dispatch_after的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆