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.
Just sharing, in Swift 4.x, I do this:
var block: DispatchWorkItem?
and then to cancel the block,
self.block?.cancel()
Try this sample project:
You can create a boolean variable
shouldCancelAnimation
and test it inside thedispatch_after
block to prevent the execution of your animation.iOS 8 and OS X Yosemite introduced
dispatch_block_cancel
that allow you to cancel them before they start executingYou declare one variable in class as follows:
Init
block
variable and provide it indispatch_after
:After that you can cancel it as follows:
Swift 3.0 Example DispatchQueue cancel or stop
Here's a general solution I wrote to cancel a dispatch_after in Swift:
Usage:
By default it runs on the main queue, but you can pass in a parameter for it to run on another queue: