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.youkuaiyun.com/htttw/article/details/7879535
中详细介绍。
最后我把完整的代码也上传上来了:
http://download.youkuaiyun.com/detail/htttw/4508503
完成!
本文介绍了一种在iOS应用中手动切换视图控制器的方法,通过直接管理视图的显示与隐藏,实现视图间的平滑过渡。具体步骤包括创建视图控制器、设置切换按钮及动画效果。
4509

被折叠的 条评论
为什么被折叠?



