-
Notifications
You must be signed in to change notification settings - Fork 471
Calling a Method After Delay
Anthony Sherbondy edited this page May 12, 2015
·
3 revisions
When prototyping or orchestrating animations, it is sometimes useful to run a method after some delay.
Because the mechanism for using Grand Central Dispatch is a bit ugly, it's useful to wrap it up into a convenient function. Put the function below into each view controller that you want to use it.
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
To run code after a delay, simply call the method, as below. Since the code in the braces is a closure, you have to use self
to refer to your class methods and variables.
// Delay for 2 seconds, then run the code between the braces.
delay(2) {
self.checkPassword()
}