Ios-多视图

来源:互联网 发布:淘宝刻假公章退货了 编辑:程序博客网 时间:2024/05/17 07:04

Ios-(4)多视图应用程序

一.     多视图应用程序的类型

1.    按钮、标签和其他控件都是UIView的子类,是视图层次的一部分。但是术语“视图”时,通常指的是具有相应类控制器UIView或子类;这些视图类型通常称为内容视图;

2.    常见的多视图应用程序就是基于导航的,tab bar,并区别工具栏;

3.    标签栏和导航栏分别位于屏幕的地步和顶部。

4.    工具栏和标签栏的区别:标签栏从多个选项之间选择一个,而工具栏多个选项之间不冲突;

二.     多视图应用程序的体系结构

1.    多视图应用程序中,根控制器的任务是获取两个或更多的视图,并根据用户输入切换视图;

2.    大部分屏幕都由一个内容视图构成,每个内容视图都由自己的控制器及输出口和操作;

3.    每个视图控制器都控制一个内容视图;

4.    窗口是ios中最基本的容器,每个应用程序有且只有一个窗口;

三.     实例:

1.    i创建一个空项目;

2.    创建三个控制器类,并取消With XIB for user interface 选项;分别命名为BIDViewController; BIDBlueViewController, BIDYellowViewController;

3.    创建三个xib文件,

4.    修改应用程序委托:在应用程序委托中添加根控制器:

.

h@class BIDViewController; @property (strong,nonatomic) BIDViewController*switchViewController;       .m #import "BIDAppDelegate.h"#import "BIDViewController.h" @implementationBIDAppDelegate @synthesizeswitchViewController; - (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{    self.window =[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    //Override point for customization after application launch.    switchViewController=[[BIDViewController alloc] initWithNibName:@"ViewController" bundle:nil];    UIView*viewF = switchViewController.view;    CGRectrect = viewF.frame;    rect.origin.y +=[UIApplication sharedApplication].statusBarFrame.size.height;    viewF.frame =rect;   //[self.window addSubview:viewF];    //?为什么要添加视图呢,为什么不把viewController设为根控制器;    self.window.rootViewController = switchViewController;       self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}

 

5.    修改根控制器:

2
.

h@class BIDBlueViewController;@class BIDYellowViewController;@property (strong,nonatomic) BIDBlueViewController*blue;@property (strong, nonatomic) BIDYellowViewController*yellow; -   (IBAction)switchViews:(id)sender;.m#import "BIDBlueViewController.h"#import "BIDYellowViewController.h"@synthesize blue;@synthesize yellow;在方法ViewDidLoad中添加:self.blue =[[BIDBlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];     [self.view insertSubview:blue.view atIndex:0];完成方法:switchViews;- (IBAction)switchViews:(id)sender{   if(self.yellow.view.superview == nil){       if(self.yellow == nil){           self.yellow = [[BIDYellowViewController alloc]initWithNibName:@"YellowViewController" bundle:nil];        }       [blue.view removeFromSuperview];       [self.view insertSubview:yellow.view atIndex:0];    }else{       if(self.blue == nil){           self.blue = [[BIDBlueViewController alloc]initWithNibName:@"BlueViewController" bundle:nil];        }       [yellow.view removeFromSuperview];       [self.view insertSubview:blue.view atIndex:0];    } }

 

6.    为三个xib文件添加源码控制器:即将xib与视图控制器关联起来

1) 将file’owner改为对应的控制器类;

2) 将控制器类的view输出指向xib的view;

7.    修改根视图控制器的xib文件:

1)  加上Toolbar工具栏,修改名字,并添加响应函数:

2)  注:工具栏与其他控件的区别:仅支持一个目标操作,而且符合条件是才能出发操作;

3)  延迟加载:黄色视图并没有一开始就加载,而是在第一次调用的时候在生成;

4)  视图控制器是否有实例的判断:self.blue.view.superview ==nil;

5)  视图的删除和添加:

[yellow.view removeFromSuperview];

[self.view insertSubview:blue.view atIndex:0];

6)  2

8.    实现内容视图的xib文件:

1)取消状态栏:status bar设为none;

四.     添加动画效果:

修改函数swithView:

- (IBAction)switchViews:(id)sender{    [UIView beginAnimations:@"ViewFlip" context:nil];    [UIView setAnimationDuration:1.25];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];       if(self.yellow.view.superview ==nil){        if(self.yellow ==nil){            self.yellow =[[BIDYellowViewController alloc] initWithNibName:@"YellowViewController" bundle:nil];        }        [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];        [blue.view removeFromSuperview];        [self.view insertSubview:yellow.view atIndex:0];    }else{        if(self.blue ==nil){            self.blue =[[BIDBlueViewController alloc] initWithNibName:@"BlueViewController" bundle:nil];        }        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];        [yellow.view removeFromSuperview];        [self.view insertSubview:blue.view atIndex:0];    }    [UIView commitAnimations]; }


1.     beginAnimation:context: ,第一个是动画块标题,第二个是指针与动画块关联的对象;

2.    setAnimationDuration:动画时间;

3.    setAnimationCurve:动画速度,是否线性

4.    setAnimationTransition:设置动画效果:

5.    commitAnimations动画块的设置完成;

原创粉丝点击