tags:

views:

1467

answers:

1

Have a simple iPhone app with a single UIViewController and two Views in one xib.

the first view is very simple with a button and upon button press the second more complex view is loaded via setting the view property on the controller.

what I would like is to animate the view swap (flip the views).

The samples I have seen all require having multiple view controllers and building a hierachy, but that would be overkill in this case, any suggestions?

+3  A: 

Make sure you declare IBOutlets for the two views in your view controller I am assuming that in your xib you have a 'container view' that occupies the whole screen, and two views of the same size that you add to this contatiner (one for each side of your 'flip'):

//Inside your .h:
IBOutlet UIView *firstView;
IBOutlet UIView *secondView;

Make sure on initial load you have the firstView show up:

-(void) viewDidLoad {
  NSAssert(firstView && seconView, @"Whoops:  Are first View and Second View Wired in IB?");
  [self.view addSubview: firstView];  //Lets make sure that the first view is shown
  [secondView removeFromSuperview];  //Lets make sure that the second View is not shown at first
}

Then you can wire up a button like this, make sure the button is wired to this metod in IB:

-(IBAction) flipButtonPressed:(id) sender {
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:0.5];
  if ([firstView superview]) {
     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
     [firstView removeFromSuperview];   
     [self.view addSubview:secondView];
  }
  else if ([secondView superview]) {
     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
     [secondView removeFromSuperview];  
     [self.view addSubview:firstView];
  }
  [UIView commitAnimations]
}
Brad Smith
Thanks, that's exactly how I have it set up and this should work without any major changes...
Franklin Munoz