gpt4 book ai didi

swift - 停止 dispatch_after

转载 作者:IT王子 更新时间:2023-10-29 05:17:51 29 4
gpt4 key购买 nike

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

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

}

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

最佳答案

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

你在类中声明一个变量如下:

var block: dispatch_block_t?

初始化block变量并在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!)

之后您可以按如下方式取消它:

dispatch_block_cancel(block!)

关于swift - 停止 dispatch_after,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28356263/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com