I'm attempting to write a function that will flash a view's background color to white then back to it's original color. What I have is:
func flashView(view: UIView) {
var sema = dispatch_semaphore_create(0)
var color = view.backgroundColor
println("Animating")
UIView.animateWithDuration(0.5, animations: { view.backgroundColor = UIColor.whiteColor() }) {
(success: Bool) in
println("First block completion")
UIView.animateWithDuration(0.5, animations: { view.backgroundColor = color }) {
(success: Bool) in
println("Nested block completion")
dispatch_semaphore_signal(sema)
}
}
println("Waiting")
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
println("Returning")
}
Now, what I'm experiencing is that the Animating
message will display followed by the Waiting
. The first animateWithDuration
block never gets executed (and needless to say the nested one does not either. However, according to Apple's own documentation the UIView.animateWithDuration
occurs on a separate thread. So, my understanding, is that the semaphore should wait, the animateWithDuration
should complete on a separate thread, and the function should return (after waiting on the semaphore that's signaled in the nested closure). What am I misunderstanding?
Thanks!