1.从头开始!
1)我们首先建立一个empty application.发现之后代理文件. delegate.h .m文件.
)然后建立一个 .switchviewController .h .m文件.先不要勾选 xib选项.这便于我们理解内部原理.
3) 在user interface 里面创建 view .命名为 switchView.xib.
4)如果现在运行,肯定是不会成功的,因为我们还缺少必要的链接操作.
连接操作如下:
i首先在 appdelegate 添加如下的代码:
#import "BIDAppDelegate.h"
#include "BIDSwitchViewController.h"
@implementation BIDAppDelegate
@synthesize switchViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.switchViewController = [[BIDSwitchViewController alloc]initWithNibName:@"SwitchView" bundle:nil];
UIView *switchView = self.switchViewController.view;
[self.window addSubview:switchView];
[self.window makeKeyAndVisible];
return YES;
}
这样子,我们便加入了 view 控制器的加载选项.
ii.然后我们需要去修改我们的switchView 里面的内容了.这点是很重要的,如果不对我们的view 和我们的viewcontroller进行连接,那么就相当于没有桥拉.
第一步:我们需要修改 xib中 file's owner 的属性.将原来的NSObject 改变为 我们的控制台:SwtichViewCOntroller .使之能够控制 VIew.
第二步:我们需要对view的 连接诶view进行连接.拖拽到 view 上面 使之连接即可.
2.多视图的创建和视图与视图之间的交互.
按照上述的创建.m h 和 xib 文件的步骤,我们创建了 blueVIewController yellowViewController 两项,共六个文件.
下面我将进行文件的连接操作.
1)对我们在1中创建的switchViewController 的项进行操作和定义为 rootView.
首先修改 appdelegate 代理文件 的 didfinish….中 添加以下的 rootView视图代码.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.switcherViewController = [[BIDSwitcherViewController alloc]initWithNibName:@"SwitchView" bundle:nil];
UIView *switchView = self.switcherViewController.view;
// CGRect switchViewFrame = switchView.frame;
//
// switchViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;
//
// switchView.frame = switchViewFrame;
[self.window addSubview:switchView];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
上述代码的意思是:把switcherViewController 作为 根视图.也就是说 app运行的时候首先加载此项.
ps:我在学习的时候由于忘记了
[self.window addSubview:switchView];
2)连接! 连接 swticherViewCOntroller 和 switcherView .
由于我们建立的时候 没有勾选 xib文件,所以我们需要 手动对这两项进行 连接.保罗 blue 和yellow这两项,后面不再赘述.
i 改变 file's owner 的class 类.原本是 NSObject 类,现在我们需要改变成它对应的控制类. switcherViewController --- switcherView
ii. 将file's owner 的 view 连接到 视图 view. 拖拽即可.
3)简历一个toolbar view.只有把此项放在根视图中,并且其他的视图显示的时候设置尺寸不要覆盖这个视图才可以.
3.对switcherView 进行处理,是他在显示的时候加载 blueView 而不是它本身.
- (void)viewDidLoad
{
self.blueViewController = [[BIDBlueViewController alloc]initWithNibName:@"BlueView" bundle:nil];
[self.view insertSubview:self.blueViewController.view atIndex:0];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
4.在 switcherViewController 中定义函数 switcherView 函数,与 toolbar 的 bar Button Item 相连接.
-(IBAction)switcherView:(id)sender
{
if (self.yellowViewController.view.superview == nil) {
if(self.yellowViewController == nil)
{
self.yellowViewController = [[BIDYellowViewController alloc]initWithNibName:@"YellowView" bundle:nil];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
}
else
{
if (self.blueViewController.view.superview == nil) {
if (self.blueViewController == nil) {
self.blueViewController = [[BIDBlueViewController alloc]initWithNibName:@"BlueView" bundle:nil];
[yellowViewController removeFromParentViewController];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}
}
}
一个简单的界面交互就成功了.当然,我们还可以添加效果.
只需要在
-(IBAction)switcherView:(id)sender 中添加相应的代码即可.
完整代码如下:
- (IBAction)switchViews:(id)sender {
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if (self.yellowViewController.view.superview == nil) {
if (self.yellowViewController == nil) {
self.yellowViewController =
[[BIDYellowViewController alloc] initWithNibName:@"YellowView"
bundle:nil];
}
[UIView setAnimationTransition:
UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[self.blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
if (self.blueViewController == nil) {
self.blueViewController =
[[BIDBlueViewController alloc] initWithNibName:@"BlueView"
bundle:nil];
}
[UIView setAnimationTransition:
UIViewAnimationTransitionFlipFromLeft
forView:self.view cache:YES];
[self.yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex:0];
}
[UIView commitAnimations];
}