iPhone之手动切换View

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





完成!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值