How to animate swap viewing on a simple iPhone app?

You have a simple iPhone app with one UIViewController and two views in one xib.

the first view is very simple with a button, and when the button is pressed, the second more complex view is loaded by setting the view property on the controller.

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

The samples I saw require multiple view controllers and hierarchy creation, but in this case it will be redundant, any suggestions?

+3
source share
1 answer

, IBOutlets . , xib " ", , , ( "" ):

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

, firstView:

-(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
}

​​, , 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];
}
+9

All Articles