Search code examples
iphoneioscocoa-touchuiviewcore-animation

iPhone UIView Animation Best Practice


What is considered best practice for animating view transitions on the iPhone?

For example, the ViewTransitions sample project from apple uses code like:

CATransition *applicationLoadViewIn = [CATransition animation];
[applicationLoadViewIn setDuration:1];
[applicationLoadViewIn setType:kCATransitionReveal];
[applicationLoadViewIn setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[[myview layer] addAnimation:applicationLoadViewIn forKey:kCATransitionReveal];

but there are also code snippets floating around the net that look like this:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:myview cache:YES];
[myview removeFromSuperview];
[UIView commitAnimations];

What is the best approach? If you could provide a snippet as well it'd be much appreciated.

NOTE: I've been unable to get the second approach to work correctly.


Solution

  • From the UIView reference's section about the beginAnimations:context: method:

    Use of this method is discouraged in iPhone OS 4.0 and later. You should use the block-based animation methods instead.

    Eg of Block-based Animation based on Tom's Comment

    [UIView transitionWithView:mysuperview 
                      duration:0.75
                       options:UIViewAnimationTransitionFlipFromRight
                    animations:^{ 
                        [myview removeFromSuperview]; 
                    } 
                    completion:nil];