views:

23

answers:

1

Hello

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 :)

A: 

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:)

mvds