How to animate View swap on simple View iPhone App?

前端 未结 1 622
死守一世寂寞
死守一世寂寞 2020-12-16 08:33

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 c

相关标签:
1条回答
  • 2020-12-16 09:00

    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];
    }
    
    0 讨论(0)
提交回复
热议问题