详解IOS中Tool Bar切换视图方法

2020-01-21 02:52:12于海丽

view source print ?


- (void)didReceiveMemoryWarning { 
 [super didReceiveMemoryWarning]; 
 if (self.firstViewController.view.superview == nil) { 
 self.firstViewController = nil; 
 } else { 
 self.secondViewController = nil; 
 } 
 } 

 

11、打开FirstView.xib文件,选择左边的File's Owner,然后在Identity Inspector中选择Class为FirstViewController;然后按住Control键从File's Owner图标拖到View,在弹出的菜单选择view。为SecondView.xib进行同样的操作,不过Class选择为SecondViewController。

12、打开FirstView.xib文件,选择View,打开Attribute Inspector,进行如下设置:

IOS,Tool,Bar,切换视图

对SecondView.xib进行同样设置,不过背景颜色设成红色。

13、此时运行程序,你会看见刚启动的时候,程序显示的绿色背景,轻触Switch Views按钮后,背景变成了红色。不断轻触按钮,背景不断变换。

14、添加切换背景的动画效果:

打开RootViewController.m,修改其中的switchViews方法如下:

view source print ?


 - (IBAction)switchViews:(id)sender { 
 [UIView beginAnimations:@"View Flip" context:nil]; 
 [UIView setAnimationDuration:1.25]; 
 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
 if (self.secondViewController.view.superview == nil) { 
 if (self.secondViewController == nil) { 
 self.secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil]; 
 } 
 [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; 
 [self.firstViewController.view removeFromSuperview]; 
 [self.view insertSubview:self.secondViewController.view atIndex:0]; 
 } else { 
 if (self.firstViewController == nil) { 
 self.firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil]; 
 } 
 [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; 
 [self.secondViewController.view removeFromSuperview]; 
 [self.view insertSubview:self.firstViewController.view atIndex:0]; 
 } 
 [UIView commitAnimations]; 
 } 

 

注意四个表示切换效果的常量:

view source print ?


UIViewAnimationTransitionFlipFromLeft 
UIViewAnimationTransitionFlipFromRight
UIViewAnimationTransitionCurlDown
UIViewAnimationTransitionCurlUp 

 

分别表示从左翻转、从右翻转、向下卷、向上卷。
运行后翻页效果如下:

IOS,Tool,Bar,切换视图 IOS,Tool,Bar,切换视图