如何取消对dispatch_after的调用?

时间:2015-12-17 00:38:51

标签: ios swift grand-central-dispatch cancellation

我正在制作一个两个按钮移动的应用程序。第一个立即移动,第二个移动1秒钟后移动。现在在我的应用程序中发生的事情是,当我在一秒钟之前单击开始按钮时,它会显示第二个按钮移动的两个按钮。我相信我要做的是,当点击按钮时,我将不得不取消dispatch_after,但是当我尝试通过编写此代码来执行此操作时:

let time1 = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW),1 * Int64(NSEC_PER_SEC))
dispatch_after(time1, dispatch_get_main_queue()) {
    // code of second button moving
}
if time1 == true
    {time1 = false}

它说我无法将其分配给dispatch_time。我该怎么办? 如果这对您有帮助,这是完整的代码:

@IBAction func startbutton(sender: UIButton) {
// code for moving the first button
let time1 = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW),1 * Int64(NSEC_PER_SEC))
dispatch_after(time1, dispatch_get_main_queue()) {
    // code of second button moving
}
if time1 == true
    {time1 = false}
}

0 个答案:

没有答案