如何在简单的View iPhone App上动画查看交换?

Fra*_*noz 3 iphone

有一个简单的iPhone应用程序,一个UIViewController和一个xib中的两个视图.

使用按钮第一个视图非常简单,按下按钮后,通过在控制器上设置视图属性来加载第二个更复杂的视图.

我想要的是动画视图交换(翻转视图).

我看到的样本都需要有多个视图控制器并构建一个层次结构,但在这种情况下,任何建议都会有些过分?

Bra*_*Guy 9

确保在视图控制器中为两个视图声明IBOutlets我假设你的xib中有一个占据整个屏幕的"容器视图",以及你添加到这个contatiner的两个相同大小的视图(每个视图一个)你'翻转'的一面):

//Inside your .h:
IBOutlet UIView *firstView;
IBOutlet UIView *secondView;
Run Code Online (Sandbox Code Playgroud)

确保在初始加载时显示第一个View:

-(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
}
Run Code Online (Sandbox Code Playgroud)

然后你可以连接这样一个按钮,确保按钮连接到IB中的这个metod:

-(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];
}
Run Code Online (Sandbox Code Playgroud)