UIView.animateWithDuration(10, delay: 20, options: .TransitionNone, animations: {
}) { (let finish) in
print("finish")
}
Like the code shown, i want to print "finish" after 20 seconds, however i get it immediately.
By the way, how many ways to do make operations delay?
First Method: NSTimer
func myDelayedFunction() {
print("delayed")
}
func myMainFunction() {
let timer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "myDelayedFunction", userInfo: nil, repeats: false)
timer.fire()
}
Second Method: performSelector
func myDelayedFunction() {
print("delayed")
}
func myMainFunction() {
performSelector("myDelayedFunction", withObject: self, afterDelay: 10)
}
Third Method: GCD
(useful if you want an anonymous function)
(adapted from this answer)
let delayInSeconds: Int64 = 3
let popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * Int64(NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue()) {
print("Delayed")
}
Fourth Method: sleep
func myMainFunction() {
sleep(10) //This will block whichever thread you're running on.
print("delayed")
}
I tend to use the first method, because I can always invalidate the timer if I need to (calling timer.invalidate
). Each method has its use case, though.
if you dont actually need to do any animation, can use GCD to accomplish this
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(20 * NSEC_PER_SEC)), dispatch_get_main_queue()){
print("finish")
};
you can make a neat little helper function as described in this answer as well
I think, there's a misconception. In your code:
UIView.animate(withDuration: 10, delay: 20, options: [], animations: {
// define animations
self.myView.alpha = 0 // as an example
}) { (finish) in
print("finish")
}
the completion handler will be called, when the animation finished. It starts after delay
seconds (20.0) and takes duration
seconds (10.0). That is, the animation should be completed after 30 seconds measured from the method call.
(You can place NSLog
statements in your code to see when things happen)
I cannot reproduce what you are experiencing, that "finish" will be printed immediately. However, I can confirm that UIView.animateWithDuration
works as expected - given the animation block is actually not empty.