Take the 2-minute tour ×

For reasons too boring to describe, I am programatically changing my application's tab bar to move to a different view. Unfortunately that is instant and we would like the normal snazy load effect you get when you would do a pushViewController on a navigation controller.

I am very inexperienced with using animations in objc c, I attempted to use some code I found here for mine:

CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[self view] cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:2.0];
[[[self nav] tabBarController] setSelectedIndex:2];
[UIView commitAnimations];

This does an animation of sorts, the nav bar mysteriously appears from above, not quite what I wanted :)

share|improve this question
I know this is old, but for what it's worth, as of iOS 4.0 you can use the new UIView transition function using blocks. I did something very similar, but called setSelectedIndex: in the completed animation block which worked out rather well. –  slycrel Nov 30 '10 at 8:15

1 Answer 1

The easiest way to get your animation to do what you want, is to first position it from the place you want it to come from (which is outside of the screen, presumably), and then start the [UIView ...] block, in which you set the new frame. So the general outline is:

CGRect newframe = theView.frame;
newframe.origin.x = 320.0f;
theView.frame = newframe;

[UIView beginAnimations:nil context:nil];
          // btw the context is just your context for animation delegate callbacks!
newframe.origin.x = 0.0f;
theView.frame = newframe;
[UIView commitAnimations];

Note that all this code is executed in an instant, so the setSelectedIndex will be set immediately as well: since there is no UIView involved there, it is not animated. To fire something past the animation, have a look at other questions. (in short: use the animationDelegate, or performSelector:afterDelay:)

share|improve this answer

Your Answer


