Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding ios UIView animation block and dispatch_async(dispatch_get_main_queue) block

I am trying to get a better understanding of this topic. Lets say I want to do some really cool animation like the following

- (void)coolAnimation 
{
    [UIView animateWithDuration: some duration
                     animations:^{ some animation }];
}

Since its an animation block, does it automatically added to the main_queue ? Or for best practice, I should always add the UI updates into the main_queue like the following.

dispatch_async(dispatch_get_main_queue(), ^{
    [self coolAnimation];
});
like image 764
Jacky So Avatar asked Dec 01 '22 15:12

Jacky So


1 Answers

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:]. It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.

In a very high-level hand-wavy kind of view of this, [UIView animateWithDuration:animations:] evaluates your block and determines functions for each animatable value within it. The OS has an animation thread that it uses as its frame timer, then at each tick it sends an update on the main thread to your animatable properties with their progressing values. (You can check the current state of these values on your presentationLayer.)

like image 50
Ian MacDonald Avatar answered Dec 06 '22 23:12

Ian MacDonald