View transitions

April 28th, 2009 by Chris Leave a reply »

Using view transitions will give a lot of beauty to your entire application. So far there are implemented 4 transitions:

  • Curl Up (UIViewAnimationTransitionCurlUp) – check Maps application on iPhone, but it’s not best example. Simply: page flip; curls a view up from the bottom
  • Curl Down (UIViewAnimationTransitionCurlDown)
  • Flip from right (UIViewAnimationTransitionFlipFromRight) - execly the same like in Weather app when you press info button; flips a view around a vertical axis
  • Flip from left (UIViewAnimationTransitionFlipFromLeft)

The code for setting a view transition is almost the same like for any other animation, here is the example:

	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1.5];
	[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:placeholder cache:YES];
	[view1 removeFromSuperview];
	[placeholder addSubview:view2];
	[view2 removeFromSuperview];
	[placeholder addSubview:view1];
	[UIView commitAnimations];

I have a view called placeholder and it’s added as a part of self.view of the view controller. view1 and view2 are two different views I want to transit go from view1 to view2 and vice verse. placeholder is of the same size as both view1 and view2. During this animation / transition placeholder flips around hiding view1 and revealing view2. During the transition, everything what is below placeholder is partialy visible. The animation takes 1.5 seconds as u set in setAnimationDuration.

Please note #1:
It’s only an animation, not real rotation / flip of placeholder let’s say it an optical illusion. UIView (what placeholder is) doesn’t have something like back and front side. view1 after transition is not hidden behind the placeholder, it has been removed, and you still see the front side of placeholder although it’s covered by view2, which is a placeholder’s subview.

Please note #2:
Unfortunately you can’t see the curl up / down transition on iPhone Simulator, it looks like fade, but it really works on real device.

Please note #3:
If view that animate’s in my case placeholder (yes placeholder animates, view1/view2 are only added/removed) is bigger than view1/view2 you will always see front site of that part although it will animate too.

Please note #4:
If you see annoying white part during animation, simply add something black below the placeholder – another view or image for example.

Fade transition

As I told you there are only 4 transitions, but many are curious how to make the fade transition. It’s very easy, simply create a normal animation which will change the alpha property for both views:

[placeholder addSubview:view2];
[placeholder addSubview:view1];
	view2.alpha = 0;
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:1.5];
	view1.alpha = 0;
	view2.alpha = 1;
	[UIView commitAnimations];

I prepared for you a project you can download and test Curl Up, Curl Down, Flip From Right, Flip From Left, Fade and also Flip From Left again but along the self.view, not placeholder.

flip1flip3flip2

xcodeproj

Download the project

Advertisement

24 comments

  1. Arunjack says:

    You did great job . Really Its very helpful for my application.
    Continue your job for iphone developers.

  2. Nick says:

    I really wish I hadn’t seen this as I really want one now! This is all very new to me and this article really opened my eyes.Thanks for sharing with us your wisdom. http://www.charmhandbags.com

  3. I would like to consider the chance of saying thanks to you for your professional guidance I have usually enjoyed visiting your site. I will be looking forward to the commencement of my school research and the whole planning would never have been complete without checking out your site. If I may be of any assistance to others, I’d personally be happy to help as a result of what I have discovered from here.

  4. Fleta Mcgray says:

    I adore that weblog structure . Exactly how had been it produced? It’s very cool!

  5. Robin says:

    What if I don’t wish to actually add/remove the 2 views. Can I just hidden/unhidden the 2 views? Doesn’t seem to work.

    Also… I’m not trying to flip the entire window… but rather… just my 2 small objects. (2 small UITextViews.)

  6. Robin says:

    What if I don’t wish to actually add/remove the 2 views. Can I just hidden/unhidden the 2 views? Doesn’t seem to work.

    Also… I’m not trying to flip the entire window… but rather… just my 2 small objects. (2 small UITextViews.)

  7. valium says:

    Youre so cool! I dont suppose Ive read something like this before. So good to search out somebody with some original thoughts on this subject. realy thank you for beginning this up. this web site is something that’s needed on the internet, somebody with just a little originality. helpful job for bringing something new to the internet!

  8. Awesome post ! Cheers for, posting on my blog man! I shall message you soon. I didn’t realise that.

  9. Thats lovely stuff you written up in here. Have been hunting for reviews on this everywhere. Great blog

  10. Thank you for another fantastic blog. Where else could I get this kind of info written in such an incite full way? I have a presentation that I am just now working on, and I have been looking for such information.

  11. Advantageously, the post is actually the sweetest topic on this registry related issue. I fit in with your conclusions and will thirstily look forward to your future updates. Just saying thanks will not just be enough, for the extraordinary clarity in your writing. I will immediately grab your rss feed to stay informed of any updates.

  12. dbonneville says:

    Why is this done in the delegate files? I’m a bit of a newb, and many of the examples I see start with creating a new project which create delegate h and m files. Can this code be pasted into my project h and m files to work along with the other code I have there?

  13. Juliaan says:

    Great tutorial, informative and to the point!!!

    How does one know when an animation is animating or has completed the animation, like a duringAnimation or animationComplete event?

  14. Alexander Stone says:

    Many many tnx:) it was better than expected:)

  15. Now, with the new simulator, CURL effects are visible!
    Thx

    • Chris says:

      Yes they are, but I don’t have time to update my tutorials. I’m busy with bureaucracy, app development, contracts…

      Chris

  16. ashish says:

    Chris thank you so much. This is a great job you are doing.
    your tutorials are very helpful for me. Saving lot of time and energy.

    Thanks once again please come up with more good ones.

    thanks
    ashish

  17. Brandon says:

    -(void)FadeAnimation {

    [UIView beginAnimations:nil context:NULL];
    //[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:animationView cache:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:1.0];
    [myView setAlpha:0];

    // Animations
    //[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

    // Commit Animation Block
    [UIView commitAnimations];

    }

    • Brandon says:

      This is what I use. This is a -(void), but everything inside can be placed in an -(IBaction). The only thing that you change is the myView, you change it to the view or sub view that you want to fade. This is good for intros splash screens. I use this with timers and have the view that fades out hide the second the animation is over.

  18. Aaryan says:

    Hi,
    Really useful example. and downloadable code.

    thanks,
    Aaryan

Leave a Reply