接上节:

1.我们用TabBarApplication项目,在Interface Builder中,我们从Livrary拖拉Tab Bar项目到Tab Bar窗口中,拉到最下面,这样就多了一个项目.

4-48.png

2.点击选中我们新添加的这个Tab Bar项目,在Attributes属性窗口,我们设置它的Badge值为5,Identifier属性为Search.看到项目的图标变成了一个放大镜.

4-49.png

3.回到Xcode,右键点击Classes,我们选择增加一个New File.选择cocoa Touch Classes,选择UIViewContoller子类.命名为SearchViewContoller.m(这里要注意要把With xib for user interface选中)

4.可以看到我们多了几个文件,我们双击我们新建立的SearchView.xib文件,添加一个Label到里面

4-50.png

5,我们回到MainWindow.xib,点击Tab Bar,选中我们新添加的项目,在它的属性窗口中我们设置NIB名字为SearchviewController.

4-51.png

6.Search Tab Bar项目的属性窗口中,设置类为SearchViewController.

4-52.png

7,保存,运行.一定要是在Xcode里面运行.

4-53.png

这个原理就比较简单了.不多说了.Charpter4本章完结.3,4章为学习Xcode的重点中的重点,一切的基础都在这里面,大家多多练习,多多实践.为后面的章节打好基础.

有同学要习题,这里有一些,大家试试看吧

1.用代码来创建一个窗口控制程序.

2.用代码来实现动态窗口.

3.用代码来实现一个事件,比如弹出.选择等等

4.多自己写几个动态警告窗口程序.

目前我们已经讲过了3种应用程序的开发:Viewbased Application,Windowbased Application,和Split Viewbased Application.还有一种比较常用的应用程序就是我们这节讲学习的Tab Bar Application.

创建一个Tab Bar Application

1.Xcode,创建一个项目,命名为TabBarApplication

2.查看项目内容,我们可以看到里面和前面所举例的项目中有所不同.里面包含了2个View控制器,(FirstViewController和SecondViewController)和3个XIB文件(MainWindow.xib,FirestView.xib和SecondView.xib).如图

4-43.png

3.查看TabBarApplicationAppDelegate.h文件

 

#import <UIKit/UIKit.h>
@interface TabBarApplicationAppDelegate : NSObject
<UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

 

#import <UIKit/UIKit.h>

@interface TabBarApplicationAppDelegate : NSObject

<UIApplicationDelegate, UITabBarControllerDelegate> {

UIWindow *window;

UITabBarController *tabBarController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

我们用UITableViewcontroller类取代了以往用的UIViewController类.功能一样,不过更具体一些.

4.当应用程序完成加载时,UITableViewcontroller实例的当前窗口被读取.这个在TabBarApplicationAppDelegate.m文件中实现

 

#import “TabBarApplicationAppDelegate.h”

@implementation TabBarApplicationAppDelegate

 

@synthesize window;

@synthesize tabBarController;

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 在当前控制器窗口添加一个tab子窗口

[window addSubview:tabBarController.view];

[window makeKeyAndVisible];

return YES;

}

5.双击MainWindow.xib文件,可以看到我们已经有了一个包含2个Tab Bar项目的窗口.

6.点击第一个Tab Bar(被标签First).在Identify Inspector窗口,我们可以看到实现的类为FirstViewController.

4-44.png

我们在Attributes Inspector 窗口可以看到,它与FirstView.xib文件进行了链接

4-45.png

7.点击第二个Tab Bar项目,在Attributes和Identify属性窗口我们可以看到和第一个项目类似的设置,只不过链接的类和xib文件变为了SeconView.

8.回到Xcode,我们先运行看看基础程序是什么样的效果.点击看大图

4-46.png

原理:

这一切都是用UITableViewcontroller类来实现的.双击MainWindow.xib文件,我们可以看到多了一个Tab Bar Controller项目.

4-47.png

Tab Bar控制器应该说是View控制器的一个集合,由两个view控制器组成.当程序加载后UITabBarViewcontroller将会一直被显示,你点击屏幕下方的2个选项,点哪个,就显示哪个.

当然我们也可以添加更多的tab选项,显示更多的页面!

 

 

 

 

前面我们已经做好了一个切分窗口,具体怎么应用我们通过下面的例子来实现,比如我们在左边列出一些电影的名字,当我们点击这些电影的时候,右边主窗口显示出相应的电影海报图片.

例子:Displaying Some Items

1.继续沿用splitViewBasedApp项目,双击DetailView.xib文件进行编辑

2.在View窗口添加一个图片应用窗口,在Attributes Inspector窗口中设置它的模式为Aspect Fit,如图

4-37.png

3.点击刚才添加的view窗口,在Size Inspector窗口中对Autosizing 进行设定

 

➤➤ X: 152
➤➤ Y: 163
➤➤ W: 463
➤➤ H: 644

 

➤➤ X: 152

➤➤ Y: 163

➤➤ W: 463

➤➤ H: 644

4-38.png

4.我们添加一些电影海报在我们的Resources文件夹内,大家可以下载这里的源程序包,里面有图片和源码,也可以自己去找几个图片来代替,注意大小就是了.

splitViewBasedApp.rar

4-39.png

5.在DetailViewcontroller.h文件中,我们需要添加红色代码:

 

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

<UIPopoverControllerDelegate, UISplitViewControllerDelegate> {

 

UIPopoverController *popoverController;

UINavigationBar *navigationBar;

id detailItem;

IBOutlet UIImageView *imageView;

}

@property (nonatomic, retain) UIPopoverController *popoverController;

@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;

@property (nonatomic, retain) id detailItem;

@property (nonatomic, retain) UIImageView *imageView;

@end

6.做好链接,按住Ctrl点击拖拽File's Owner项目到图片窗口,选择imgageView

4-40.png

7.在RootViewController.m文件中添加红色代码:

 

#import “RootViewController.h”

#import “DetailViewController.h”

@implementation RootViewController

@synthesize detailViewController;

NSMutableArray *listOfMovies;

- (void)viewDidLoad {

//---初始化一个数组,后面的图片名称可以根据你的需求自己修改---

listOfMovies = [[NSMutableArray alloc] init];

[listOfMovies addObject:@“Training Day”];

[listOfMovies addObject:@“Remember the Titans”];

[listOfMovies addObject:@“John Q.”];

[listOfMovies addObject:@“The Bone Collector”];

[listOfMovies addObject:@“Ricochet”];

[listOfMovies addObject:@“The Siege”];

[listOfMovies addObject:@“Malcolm X”];

[listOfMovies addObject:@“Antwone Fisher”];

[listOfMovies addObject:@“Courage Under Fire”];

[listOfMovies addObject:@“He Got Game”];

[listOfMovies addObject:@“The Pelican Brief”];

[listOfMovies addObject:@“Glory”];

[listOfMovies addObject:@“The Preacher’s Wife”];

//---设置 title---

self.navigationItem.title = @“Movies”;

[super viewDidLoad];

self.clearsSelectionOnViewWillAppear = NO;

}

- (NSInteger)tableView:(UITableView *)aTableView

numberOfRowsInSection:(NSInteger)section {

// 返回我们有多少行的数值.

//return 10;

return [listOfMovies count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @“CellIdentifier”;

// Dequeue or create a cell of the appropriate type.

UITableViewCell *cell = [tableView

dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellIdentifier] autorelease];

cell.accessoryType = UITableViewCellAccessoryNone;

}

// Configure the cell.

//cell.textLabel.text = [NSString stringWithFormat:@“Row %d”, indexPath.row];

cell.textLabel.text = [listOfMovies objectAtIndex:indexPath.row];

return cell;

}

- (void)tableView:(UITableView *)aTableView

didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

/*

When a row is selected, set the detail view controller’s detail item to

the item associated with the selected row.

*/

//detailViewController.detailItem =

// [NSString stringWithFormat:@“Row %d”, indexPath.row];

detailViewController.detailItem =  [NSString stringWithFormat:@“%@“,

[listOfMovies objectAtIndex:indexPath.row]];

}

- (void)dealloc {

//施放内存很重要啊,前面不提是因为那些小程序占用内存少,如果是图片和电影那就大了,你不想你的程序用了几个后,机器慢如蜗牛.iPad,iPhone自由256,和512的内存.

  [listOfMovies release];

[super dealloc];

}

8.在DetailViewController.m文件中添加如下红色代码:

 

#import “DetailViewController.h”

#import “RootViewController.h”

@implementation DetailViewController

@synthesize navigationBar, popoverController, detailItem;

@synthesize imageView;

/*

这段代码用来控制气泡窗口的显示和取消.

*/

- (void)setDetailItem:(id)newDetailItem {

if (detailItem != newDetailItem) {

[detailItem release];

detailItem = [newDetailItem retain];

// Update the view.

navigationBar.topItem.title = [detailItem description];

NSString *imageName = [NSString

stringWithFormat:@“%@.jpg”,navigationBar.topItem.title];

 

imageView.image = [UIImage imageNamed:imageName];

}

if (popoverController != nil) {

[popoverController dismissPopoverAnimated:YES];

}

}

9.运行,看看效果.

4-41.png

4-42.png

有成就感吧,大家!继续努力,我们来了解一下这是怎么实现的

首先看前面那一大段图片定义代码:巴拉巴拉....

- (void)viewDidLoad {

//---initialize the array---

listOfMovies = [[NSMutableArray alloc] init];

[listOfMovies addObject:@“Training Day”];

[listOfMovies addObject:@“Remember the Titans”];

[listOfMovies addObject:@“John Q.”];

[listOfMovies addObject:@“...................不写了,节约版面

.............

我们用tableView:numberOfRowsInSection:方法来返回我们需要显示的行数值,和数组的大小

 

- (NSInteger)tableView:(UITableView *)aTableView

numberOfRowsInSection:(NSInteger)section {

// Return the number of rows in the section.

//return 10;

return [listOfMovies count];

}

用tableView:cellForRowAtIndexPath:方法对列表中的每个项目定义或者赋予数值,以便列表窗口的显示:

 

- (UITableViewCell *)tableView:(UITableView *)tableView

cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @“CellIdentifier”;

// Dequeue or create a cell of the appropriate type.

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellIdentifier] autorelease];

cell.accessoryType = UITableViewCellAccessoryNone;

}

// Configure the cell.

//cell.textLabel.text = [NSString stringWithFormat:@“Row %d”, indexPath.row];

cell.textLabel.text = [listOfMovies objectAtIndex:indexPath.row];

return cell;

}

当我们在列表窗口对项目进行选择的时候,我通过DetailViewController给出他们的属性值detailItem

 

- (void)tableView:(UITableView *)aTableView

didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

/*

When a row is selected, set the detail view controller’s

detail item to the item associated with the selected row.

*/

//detailViewController.detailItem =

// [NSString stringWithFormat:@“Row %d”, indexPath.row];

detailViewController.detailItem = [NSString stringWithFormat:@“%@“,

[listOfMovies objectAtIndex:indexPath.row]];

}

在DetailViewController.m文件中,我们定义setDetailItem:方法用来显示我们的每张图片:

 

- (void)setDetailItem:(id)newDetailItem {

if (detailItem != newDetailItem) {

[detailItem release];

detailItem = [newDetailItem retain];

// Update the view.

navigationBar.topItem.title = [detailItem description];

NSString *imageName = [NSString

stringWithFormat:@“%@.jpg”,navigationBar.topItem.title];

imageView.image = [UIImage imageNamed:imageName];

}

if (popoverController != nil) {

[popoverController dismissPopoverAnimated:YES];

}

}

由此实现我们要实现的东西.

端午节来了,放假了,周末了,无心工作学习啊,为什么假期这么频繁呢?

从iSO3.2开始,我们就可以在应用程序中开始切分窗口应用了.这个大量用户基于web的程序,比如在线杂志,消息类软件.简单一点就是这边选择,那边显示..

试试看:

1.用Xcode创建一个Split View-based Application,有单独的一个选项,不再是view-based Application了.命名为splitViewBasedApp,如图:

4-27.png

2.打开文件的Classes文件夹和Resources文件夹,注意这里可以看到出现了两个view控制类,RootViewController和DetailViewController.

4-28.png

3.运行一下看看,C+R,我们可以看到在模拟器中显示出了这样的画面,一边是选项,一边是内容

4-29.png

换个方向看看,左边的选项变成按钮显示了

4-30.png

它是怎么实现的呢?

这个好玩的一个地方就是在我们旋转屏幕的时候所发生的变化,当我们横向的时候,左边会分割窗口的一部分用户显示列表,而竖向的时候会隐藏到顶部的一个按钮中.

首先我们来看看splitViewBasedAppAppDelegate.h的内容:

 

#import <UIKit/UIKit.h>
@class RootViewController;
@class DetailViewController;
@interface splitViewBasedAppAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UISplitViewController *splitViewController;
RootViewController *rootViewController;
DetailViewController *detailViewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic,retain) IBOutlet UISplitViewController *splitViewController;
@property (nonatomic,retain) IBOutlet RootViewController *rootViewController;
@property (nonatomic,retain) IBOutlet DetailViewController *detailViewController;
@end

 

#import <UIKit/UIKit.h>

@class RootViewController;

@class DetailViewController;

@interface splitViewBasedAppAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;

UISplitViewController *splitViewController;

RootViewController *rootViewController;

DetailViewController *detailViewController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic,retain) IBOutlet UISplitViewController *splitViewController;

@property (nonatomic,retain) IBOutlet RootViewController *rootViewController;

@property (nonatomic,retain) IBOutlet DetailViewController *detailViewController;

@end

这里面包含一个view 控制对象UiSplitVewController(splitViewController)和两个view控制类(rootViewController和detailViewController).用这个对象来实现我们主从页面的对接.

接下来,我们来看splitViewBasedAppAppDelegate.m文件

#import “splitViewBasedAppAppDelegate.h”

#import “RootViewController.h”

#import “DetailViewController.h”

@implementation splitViewBasedAppAppDelegate

@synthesize window, splitViewController, rootViewController, detailViewController;

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 分割窗口并且显示出来.

[window addSubview:splitViewController.view];

[window makeKeyAndVisible];

return YES;

}

- (void)applicationWillTerminate:(UIApplication *)application {

// Save data if appropriate

}

- (void)dealloc {

[splitViewController release];

[window release];

[super dealloc];

}

@end

当这个应用程序被加载的时候,自动加载splitViewController对象到显示窗口.

双击MainWindow.xib文件,我看到一个叫Split View Controller的新窗口,里面包含了2个窗口控制器.回到MainWindow.xib文件,用列表显示模式,我们可以看到下面有一些选项,其中就有Split View Controller,里面有(如图)

➤➤ Navigation Controller(导航控制器)

➤➤ Detail View Controller(主页面控制器)

4-31.png

这个Navigation Controller控制器主要负责左边这个分割窗口的应用.它由一个导航栏和一个根目录控制器组成

4-32.png

根目录控制器(Root View Controller)用RootViewController类来控制4-33.png

主窗口控制器(Detail View Controller)来控制右边分割窗口的应用.

4-34.png

主窗口控制器(Detail View Controller)我们用DetailViewController类来控制

4-35.png

右键点击Split View Based App App Delegate项目,瓦萨,东西真多啊.我们可以看到很多 对分割窗口应用的链接定义

4-36.png

让我们看看包含在Split View Controller中的两种视图控制器RootViewController和DetailViewController.

我们来看RootViewController.h文件

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface RootViewController : UITableViewController {

DetailViewController *detailViewController;

}

@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;

@end

要注意的是RootViewController类是从UITableViewcontroller类继承下来的,而UITableViewcontroller类是UIViewController类的一个子类.这个我们后面会详细在Table View的时候讲.

在RootViewController.m文件中

#import “RootViewController.h”

#import “DetailViewController.h”

@implementation RootViewController

@synthesize detailViewController;

/*

---Other commented out code are omitted from this code listing---

*/

- (void)viewDidLoad {

[super viewDidLoad];

self.clearsSelectionOnViewWillAppear = NO;

}

// Ensure that the view controller supports rotation and

// that the split view can therefore show in

// both portrait and landscape.

- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation {

return YES;

}

// The size the view should be when presented in a popover.

- (CGSize)contentSizeForViewInPopoverView {

return CGSizeMake(320.0, 600.0);

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView {

// Return the number of sections.

return 1;

}

- (NSInteger)tableView:(UITableView *)aTableViewnumberOfRowsInSection:NSInteger)section {

// Return the number of rows in the section.

return 10;

}

- (UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @“CellIdentifier”;

// Dequeue or create a cell of the appropriate type.

UITableViewCell *cell = [tableView

dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellIdentifier] autorelease];

cell.accessoryType = UITableViewCellAccessoryNone;

}

// Configure the cell.

cell.textLabel.text =

[NSString stringWithFormat:@“Row %d”, indexPath.row];

return cell;

}

- (void)tableView:(UITableView *)aTableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath {

/*

When a row is selected, set the detail view controller’s

detail item to the item associated with the selected row.

*/

detailViewController.detailItem =

[NSString stringWithFormat:@“Row %d”, indexPath.row];

}

- (void)didReceiveMemoryWarning {

// Releases the view if it doesn’t have a superview.

[super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images,

// etc that aren’t in use.

}

- (void)viewDidUnload {

// Relinquish ownership of anything that

// can be recreated in viewDidLoad or on demand.

// For example: self.myOutlet = nil;

}

- (void)dealloc {

[super dealloc];

}

@end

这里面用到了很多方法,我说些简单的

➤➤ contentSizeForViewInPopoverView — 定义显示大小.

➤➤ numberOfSectionsInTableView: — 确定显示的数量

➤➤ tableView:numberOfRowsInSection: — 确定显示行的数量

➤➤ tableView:cellForRowAtIndexPath: — 每一行的内容.

➤➤ tableView:didSelectRowAtIndexPath: — 用户选择控制.

下面我们再来看看DetailsViewcontroller.h文件

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

<UIPopoverControllerDelegate, UISplitViewControllerDelegate> {

UIPopoverController *popoverController;

UINavigationBar *navigationBar;

id detailItem;

}

@property (nonatomic, retain) UIPopoverController *popoverController;

@property (nonatomic, retain) IBOutlet UINavigationBar *navigationBar;

@property (nonatomic, retain) id detailItem;

@end

值得注意的是DetailViewController类遵从下面的这些协议:

➤➤ UIPopoverControllerDelegate —对气泡栏,就是变成竖式之后隐藏起来的那个列表窗口的控制

➤➤ UISplitViewControllerDelegate — 当方向改变的时候,是否显示气泡窗口的控制

这节很长啊.......我们再来看DetailsViewController.m文件:

#import “DetailViewController.h”

#import “RootViewController.h”

@implementation DetailViewController

@synthesize navigationBar, popoverController, detailItem;

/*

---Other commented out code are omitted from this code listing---

*/

/*

When setting the detail item, update the view and dismiss

the popover controller if it’s showing.

*/

- (void)setDetailItem:(id)newDetailItem {

if (detailItem != newDetailItem) {

[detailItem release];

detailItem = [newDetailItem retain];

// Update the view.

navigationBar.topItem.title = [detailItem description];

}

if (popoverController != nil) {

[popoverController dismissPopoverAnimated:YES];

}

}

- (void)splitViewController:(UISplitViewController*)svc

willHideViewController:(UIViewController *)aViewController

withBarButtonItem:(UIBarButtonItem*)barButtonItem

forPopoverController:(UIPopoverController*)pc {

barButtonItem.title = @“Root List”;

[navigationBar.topItem setLeftBarButtonItem:barButtonItem

animated:YES];

self.popoverController = pc;

}

// Called when the view is shown again in the split view,

// invalidating the button and popover controller.

- (void)splitViewController:(UISplitViewController*)svc

willShowViewController:(UIViewController *)aViewController

invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {

[navigationBar.topItem setLeftBarButtonItem:nil animated:YES];

self.popoverController = nil;

}

// Ensure that the view controller supports rotation and that the

// split view can therefore show in both portrait and landscape.

- (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation {

return YES;

}

- (void)viewDidUnload {

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

self.popoverController = nil;

}

- (void)dealloc {

[popoverController release];

[navigationBar release];

[detailItem release];

[super dealloc];

}

@end

这里有2个对定义UiSplitVewControllerDelegate协议十分重要的事件需要注意:

 

➤➤ splitViewController:willHideViewController:withBarButtonItem:forPopover-

Controller: --当ipad改成竖方向,气泡窗口被显示,列表窗口隐藏.

➤➤ splitViewController:willShowViewController:invalidatingBarButtonItem: — 当ipad改成横向,气泡窗口被隐藏,列表窗口显示.

这节太长了,花了我一下午的时间来弄...呵呵,大家把代码多看看,一条一条的理解,这个还是比较简单的.后面的太复杂,我都看着头麻麻的!

今天重庆天气真适合睡觉啊!!!!!!!!

动态窗口切换

前面一节我们已经搞定了在不同的窗口切换,但是它还不够,在很多iPad或者iphone的应用中动态的显示才能然我们的程序更加的美观,界面更加的友好.因此我们在切换窗口的时候加入动态的切换能使我们的程序更完美,来试试吧!

例子:动态的切换Animating the Transitions

1.用到上一节的例子MySecondViewController,在MySecondViewControlle.m文件中加入下面红色代码:

 

-(IBAction) buttonClicked: (id) sender{
//---add the view of the view controller to the current View---
viewController = [[HelloWorldViewController alloc]
initWithNibName:@“HelloWorldViewController”
bundle:nil];
[UIView beginAnimations:@“flipping view” context:nil];

 

-(IBAction) buttonClicked: (id) sender{

//---add the view of the view controller to the current View---

viewController = [[HelloWorldViewController alloc]

initWithNibName:@“HelloWorldViewController”

bundle:nil];

[UIView beginAnimations:@“flipping view” context:nil];

 

[UIView setAnimationDuration:1];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft

forView:self.view cache:YES];

[self.view addSubview:viewController.view];

[UIView commitAnimations];

}

2.在HelloWorldViewController.m文件中,加入下面的红色代码:

 

-(IBAction) btnClicked:(id) sender{

[UIView beginAnimations:@“flipping view” context:nil];

[UIView setAnimationDuration:1];

[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight

forView:self.view.superview cache:YES];

[self.view removeFromSuperview];

[UIView commitAnimations];

}

3.保存,C+R,运行点击Ok按钮,看看我们界面的切换的变化.如图:

4-25.png      4-26.png

嘿嘿,怎么样?是要比上一节生硬的切换好玩吧!

这个是怎么样实现的呢?原理解释:

这是由UIView类里面的beginAnimation:方法来现实的,就是这句

[UIView beginAnimations:@“flipping view” context:nil];

并且用setAnimationDuration:方法来制定动态的时间:(这里我们定义的是1秒钟,都是以秒为单位)

[UIView setAnimationDuration:1];

再用到setAnimationCurve:方法来设置动画的旋转曲度变化

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

我们可以根据后面的常量定义来修改动画的样式:

 

➤➤ UIViewAnimationCurveEaseInOut — 慢慢的开始,快速的过程,最后完成时刻又慢慢的结束

➤➤ UIViewAnimationCurveEaseIn — 慢慢的开始,直接加速到结束

 

➤➤ UIViewAnimationCurveEaseOut — 快速的开始,然后慢慢的结束

➤➤ UIViewAnimationCurveLinear — 匀速变化

大家可以自己修改后,多试试看,so easy!

通过setAnimationTransition:方法可以定义在动画的类型

 

 

 

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft

forView:self.view cache:YES];

看上面这句,主要是定义你动画的样式的,比如右进左出啊,这些

cache:参数定义是否将当前页面定义为一副图画进行动画操作,下面的这些常数可以定义我们动画的样式:

 

➤➤ UIViewAnimationTransitionNone — 没有过渡

➤➤ UIViewAnimationTransitionFlipFromLeft —翻转一个视图从左向右

➤➤ UIViewAnimationTransitionFlipFromRight — 翻转一个视图从右到左

➤➤ UIViewAnimationTransitionCurlUp — 从上卷动

➤➤ UIViewAnimationTransitionCurlDown — 从下卷动

在这个动画的结尾,需要用到commitAnimations:方法

[UIView commitAnimations];

HelloWorldViewController中动画的显示与在MySecondViewControlle中的类似,除了在父子页面的定义,就是说是哪个是父窗口,哪个是子窗口self.view.superview:

 

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight

forView:self.view.superview cache:YES];

About Me

我是一个超级苹果迷+老乔的fans和Google的簇拥,一切新奇的事物我都好奇,摄影、运动、旅游、读书、电影、游戏都是我的爱好,荒淫大家给我留言,发邮件,请我吃饭、带我旅游(也不要忘记了我的领导同志!)……

« 2012年2月 »
   1234
567891011
12131415161718
19202122232425
26272829   

微博

统计

文章:119篇
评论:95条 (0条Spam)
相册:11个 (339张图片)
主题:InSense theme