简体   繁体   English

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

[英]How to animate View swap on simple View iPhone App?

Have a simple iPhone app with a single UIViewController and two Views in one xib. 有一个简单的iPhone应用程序,一个UIViewController和一个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? 我看到的样本都需要有多个视图控制器并构建一个层次结构,但在这种情况下,任何建议都是过度杀伤?

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

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

Make sure on initial load you have the firstView show up: 确保在初始加载时显示第一个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
}

Then you can wire up a button like this, make sure the button is wired to this metod in IB: 然后你可以连接这样一个按钮,确保按钮连接到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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM