iPhone之手动切换View

来源:互联网 发布:啥软件可以看禁播动漫 编辑:程序博客网 时间:2024/04/30 15:31

iPhone之手动切换View



我们之前切换View使用的方法是UINavigationController,今天我们介绍手动切换View:


切换的原理很简单:

有一个根UIViewController类,其中包含了要切换的那些View对应的ViewController,切换时先删除当前的View,然后调用 insertSubview 添加切换后的View,完成切换。




1.

首先创建一个View-based Application,名称是 Switch:





2.

如下图,新建两个UIViewController subclass,分别是 FirstViewController 和 SecondViewController:






3.

在 SwitchViewController.xib 中增加一个切换按钮:




修改 FirstViewController.xib 和 SecondViewController.xib 以区分它们即可,如下图:

FirstViewController.xib:

SecondViewController.xib:


可以在 ViewController 的 viewDidLoad 方法中调用:

self.view.backgroundColor = [UIColor yellowColor];

来改变View的背景颜色为黄色





4.

修改 SwitchViewController.h 如下:

////  SwitchViewController.h//  Switch////  Created by HuTao on 8/18/12.//  Copyright __MyCompanyName__ 2012. All rights reserved.//#import <UIKit/UIKit.h>@class FirstViewController;@class SecondViewController;@interface SwitchViewController : UIViewController{FirstViewController * firstViewController;SecondViewController * secondViewController;}@property (retain, nonatomic) FirstViewController * firstViewController;@property (retain, nonatomic) SecondViewController * secondViewController;-(IBAction)btnSwitchView:(id)sender;@end


修改 SwitchViewController.m 如下:

////  SwitchViewController.m//  Switch////  Created by HuTao on 8/18/12.//  Copyright __MyCompanyName__ 2012. All rights reserved.//#import "SwitchViewController.h"#import "FirstViewController.h"#import "SecondViewController.h"@implementation SwitchViewController@synthesize firstViewController;@synthesize secondViewController;-(IBAction)btnSwitchView:(id)sender{if(self.secondViewController == nil)  {  //SecondViewController * temp = [[SecondViewController alloc] init] 也可以;                  SecondViewController * temp = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];                  self.secondViewController = temp;  [temp release];  }  [UIView beginAnimations:@"View Flip" context:nil];  [UIView setAnimationDuration:1.25];  [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  UIViewController * coming = nil;  UIViewController * going = nil;  UIViewAnimationTransition transition;  if (self.firstViewController.view.superview == nil)   {          coming = firstViewController;  going = secondViewController;  //由右到左transition = UIViewAnimationTransitionFlipFromRight;                  }  else  {  coming = secondViewController;  going = firstViewController;  //由左到右transition = UIViewAnimationTransitionFlipFromLeft;  }  [UIView setAnimationTransition:transition forView:self.view cache:YES];  [coming viewWillAppear:YES];  [going viewWillDisappear:YES];  [going.view removeFromSuperview];  [self.view insertSubview:coming.view atIndex:0];  [coming viewDidAppear:YES];  [going viewDidDisappear:YES];  //提交Animation[UIView commitAnimations]; }// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad{[super viewDidLoad];FirstViewController * temp = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];  self.firstViewController = temp;  [self.view insertSubview:temp.view atIndex:0];  [temp release]; }- (void)viewDidUnload{firstViewController = nil;secondViewController = nil;}- (void)dealloc{[super dealloc];[firstViewController release];[secondViewController release];}@end


上面只演示了两个View相互切换,但更多的View切换的原理是相同的





5.

在Interface Builder中将 Switch 和Touch Up Inside 的回调函数连接起来




6.

运行结果如下:

切换前:



切换中:



切换后:






关于动画的部分将会在:

http://blog.csdn.net/htttw/article/details/7879535

中详细介绍。




最后我把完整的代码也上传上来了:

http://download.csdn.net/detail/htttw/4508503





完成!

原创粉丝点击