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.
Anyway the "Block" method is preffered now-a-days. I will explain the simple block below.
Consider the snipped below. bug2 and bug 3 are imageViews. The below animation describes an animation with 1 second duration after a delay of 1 second. The bug3 is moved from its center to bug2's center. Once the animation is completed it will be logged "Center Animation Done!".
Hope that's clean!!!
I found a good tutorial in this link. Hope this will be helpful for some one.
uiview-animation-tutorial
I have been using the latter for a lot of nice lightweight animations. You can use it crossfade two views, or fade one in in front of another, or fade it out. You can shoot a view over another like a banner, you can make a view stretch or shrink... I'm getting a lot of mileage out of
beginAnimation
/commitAnimations
.Don't think that all you can do is:
Here is a sample:
As you can see, you can play with alpha, frames, and even sizes of a view. Play around. You may be surprised with its capabilities.
We can animate images in ios 5 using this simple code.
In the
UIView
docs, have a read about this function for ios4+From the UIView reference's section about the
beginAnimations:context:
method:Eg of Block-based Animation based on Tom's Comment