Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does animateWithDuration:animations: block main thread?

I have connected the two methods below to separate buttons in my UI but have noticed that after pressing the "VERSION 1" button that I could not press the button again until the animation duration within the method had ended. My understanding was that the animation uses its own thread so as not to block the main application.

// VERSION 1 -(IBAction)fadeUsingBlock {     NSLog(@"V1: Clicked ...");     [myLabel setAlpha:1.0];     [UIView animateWithDuration:1.5 animations:^{         [myLabel setAlpha:0.0];     }]; } 

The older style version (below) does allow the button to be repressed before the animation timer ends, simply resetting the timer to start again. Should these both work the same, am I missing something or has there been a change in operation between 3.2 and 4?

// VERSION 2 -(IBAction)fadeUsingOld {     NSLog(@"V2: Clicked ...");     [myLabel setAlpha:1.0];     [UIView beginAnimations:nil context:nil];     [UIView setAnimationDuration:1.5];     [myLabel setAlpha:0.0];     [UIView commitAnimations]; } 

Cheers gary

like image 856
fuzzygoat Avatar asked Jul 13 '10 13:07

fuzzygoat


2 Answers

Animating with blocks doesn't block the main thread. I think the behavior you're seeing is because, by default, user interaction is disabled duration animation with the new block calls. You can override this by passing UIViewAnimationOptionAllowUserInteraction (calling animationWithDuration:delay:options:animations:completion), like this:

-(IBAction) fadeUsingBlock {     NSLog(@"V1: Clicked ...");     [myLabel setAlpha:1.0];     [UIView animateWithDuration:1.5                            delay:0                         options:UIViewAnimationOptionAllowUserInteraction                      animations:^{                          [myLabel setAlpha:0.0];                      }                      completion:nil]; } 
like image 192
zpasternack Avatar answered Sep 23 '22 00:09

zpasternack


For animateWithDuration:, the class reference doesn't say anything about threading, so I am not sure.

For beginAnimations:context: and commitAnimation:, yeah, they run in a separate thread UIView class Reference.

Some of the property changes to view objects can be animated—for example, setting the frame, bounds, center, and transform properties. If you change these properties in an animation block, the changes from the current state to the new state are animated. Invoke the beginAnimations:context: class method to begin an animation block, set the properties you want animated, and then invoke the commitAnimations class method to end an animation block. The animations are run in a separate thread and begin when the application returns to the run loop. Other animation class methods allow you to control the start time, duration, delay, and curve of the animations within the block.

like image 25
vodkhang Avatar answered Sep 20 '22 00:09

vodkhang