---
title: "新风作浪博客学习(六)ios 视图切换翻页效果 ."
author: "android_zhang"
date: "2013-06-05T11:18:03Z"
tags: ["iOS"]
categories: ["移动开发"]
slug: "ios"
draft: false
lastmod: "2013-01-16T22:00:52Z"
id: 1771174
url: "https://android-zhang.iteye.com/blog/1771174"
---

本文写的是视图切换，涉及到的内容有

1.实现代码添加Navigation Bar  Toolbal；

2.实现在Navigation Bar和Toolbar上用代码添加Bar Button Item；

3.UIView层面的简单动画效果





![](http://dl.iteye.com/upload/attachment/0079/3996/c892baff-bd11-3b89-8d52-d53cef6b99cc.png)



![](http://dl.iteye.com/upload/attachment/0079/3998/df183afd-a097-35e6-9813-3d936af07abd.png)



![](http://dl.iteye.com/upload/attachment/0079/4000/18913560-45d4-368a-b16c-b4ab69f476e3.png)



![](http://dl.iteye.com/upload/attachment/0079/4002/13490a20-40a2-3cd2-a63c-1ab545b44a59.png)




开始制作：

1.创建一个新工程叫NVDemo； File->New->Project ->single View Application -> next


![](http://dl.iteye.com/upload/attachment/0079/4004/db5885ae-c9e7-3f69-b857-18af7eaf6d4a.png)







2.在新建两个ViewController，分别为FirstViewController和SecondViewController，顺便把XIB一块生成好


![](http://dl.iteye.com/upload/attachment/0079/4006/9f057849-88d8-325b-b6c6-233e2c6309ba.png)








3.首先在视图上添加导航栏和导航按钮，经测试导航栏上只能添加两个导航按钮，和设置一个title标题；

我们还需要知道的一个常识是NavigationBar  ToolBar  Tab  Bar  都是44像素，所以在设置他们宽度时候他们的高度设置成44

还有一个通知栏，显示电量信息信号的地方是20像素；

```java
- (void)viewDidLoad
{
    [super viewdidload];
	// Do any additional setup after loading the view, typically from a nib.
    
//    创建导航栏,44像素
    UINavigationBar *navigationBar = [[uinavigationbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    
//    需要在导航栏上创建按钮，所以先创建一个导航栏集合
    UINavigationItem *navagationItem = [[uinavigationitem alloc] initWithTitle:@"导航栏"];
    
    UIBarButtonItem *leftButton = [[uibarbuttonitem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(pageDown:)];
    
    UIBarButtonItem *rightButton = [[uibarbuttonitem alloc]initWithTitle:@"右测试"
                                                                   style:UIBarButtonItemStyleDone
                                                                  target:self
                                                                  action:@selector(leftpage:)];
    [navigationbar pushnavigationitem:navagationitem animated:yes];
    
    [navagationitem setleftbarbuttonitem:leftbutton animated:yes];
    [navagationitem setrightbarbuttonitem:rightbutton animated:yes];
    
    [self.view addsubview:navigationbar];
     
    UIToolbar *toolBar = [[uitoolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
    
    NSMutableArray *toolBarArray = [nsmutablearray array];
     
    [toolbararray addobject:[[uibarbuttonitem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl
                                                                           target:self
                                                                           action:@selector(switchLoadView:)]];
    
    [toolbararray addobject:[[uibarbuttonitem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                                                         target:self
                                                                         action:@selector(rightpage:)]];
    
    [toolbararray addobject:[[uibarbuttonitem alloc]initWithTitle:@"MyTitle"
                                                            style:UIBarButtonItemStylePlain
                                                           target:self
                                                           action:nil]];
    
    
    
    //UIToolBar上添加图像
    [toolbararray addobject:[[uibarbuttonitem alloc]initWithImage:[uiimage imagenamed:@"myimage.png"]
                                                            style:UIBarButtonItemStylePlain
                                                           target:self
                                                           action:nil]];
       
    [toolbar setitems:toolbararray animated:yes];  
    [self.view addsubview:toolbar];
      
}
```



   ;涉及到一个压栈的操作，把navigationItem压到navigationBar里，导航栏上只能添加左右两个按钮；所以是setLeftBarButtonItem 和 setRightBarButtonItem，最后再将navigationBar添加到视图之上；

         在Toolbar上添加可以添加各种按钮，创建一个可变数组，把添加的按钮全部放到数组上，;将数组里按钮集合添加到了toolBar上面，选取图片的时候素材没选好，所以显示出来的图片那个按钮效果不是太好








4.接下来说的是按钮事件，因为需要用到FirstViewController和SecondViewController，在RootViewController.m添加上他们的头文件，为了区别确实是两个视图的切换，在他们的ViewDidLoad函数中初始化视图的背景颜色，

self.view.backgroundColor = ;  和self.view.backgroundColor = ;

按钮时间再次也不做过多解释，全部写在注释里了，其他几个都一样，只是修改了动画效果，变化不大，详情可下载源代码研究一下；

```java
-(void) switchLoadView:(id)sender
{

//开始一个动画    
    [uiview beginanimations:@"curl" context:nil];
//    设置动画方式，开始和结束时动画效果比较慢
    [uiview setanimationcurve:uiviewanimationcurveeaseinout];
//    动画持续时间
    [uiview setanimationduration:1.25];
//    设置动画效果，向上翻页
    [uiview setanimationtransition:uiviewanimationtransitioncurlup forview:self.view cache:yes];    
    
//    首先判断firstView视图父视图是否为空
    if (firstView.view.superview == nil) 
    {
//        父视图为空，在判断他的子视图是否为空，如果为空在创建一个视图加载上面
        if (firstView.view == nil) {
            FirstViewController *firstViewDemo = [[firstviewcontroller alloc] initWithNibName:@"FirstViewController" bundle:nil];
            firstView = firstViewDemo;
        }
//        把seconView视图从父视图中移除
        [secondview.view removefromsuperview];
//        在当前视图插入子视图firstView的视图
        [self.view insertsubview:firstview.view atindex:0];
    }
       
    else {
            if (secondView.view == nil) 
            {
                SecondViewController *secondViewDemo = [[secondviewcontroller alloc] initWithNibName:@"SecondViewController" bundle:nil];
                secondView = secondViewDemo;
            }
            [firstview.view removefromsuperview];
            [self.view insertsubview:secondview.view atindex:0];
        }
//       动画结束
    [uiview commitanimations];
}
```





咱在这在研究一个问题，在RootAppDelegate.m中我们先看看系统生成的代码

```java
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[uiwindow alloc] initWithFrame:[[uiscreen mainscreen] bounds]];
    // Override point for customization after application launch.   
    self.viewController = [[rootviewcontroller alloc] initWithNibName:@"RootViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makekeyandvisible];
    return YES;
}
```




系统直接加载的就是的RootViewController的视图，也就是弹出第一个界面是RootViewController.xib,假如说我想第一个就显示的FirstViewController控制的视图怎么办？ 我们就可以在这个函数中进行重写

在delegateApp.h中@class RootViewController后面添加@class FirstViewController;此处声明一个类，在这样当我们添加@property (strong, nonatomic) FirstViewController *fTestViewController;才不会报错；




只需修改两行代码，此处注释原先加载RootViewController视图的代码，可能是我命名的不够合理，RootViewController和rootViewController要区别开，一个工程建立的，一个是系统本身自动生成的


```java
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[uiwindow alloc] initWithFrame:[[uiscreen mainscreen] bounds]];
    // Override point for customization after application launch.
//    self.viewController = [[rootviewcontroller alloc] initWithNibName:@"RootViewController" bundle:nil];
//    self.window.rootViewController = self.viewController;
     
    self.fTestViewController = [[firstviewcontroller alloc] initWithNibName:@"FirstViewController" bundle:nil];
//  把根控制视图设置成fTestViewController
    self.window.rootViewController = self.fTestViewController;
    
    [self.window makekeyandvisible];
    return YES;
}
```



然后再到我们的FirstViewController.m的ViewDidLoad函数先添加几行代码，以示区别

```java
- (void)viewDidLoad
{
    [super viewdidload];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [uicolor yellowcolor];
    
    UIToolbar *toolBar = [[uitoolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    
   NSMutableArray *toolBarArray = [nsmutablearray array];
    [toolbararray addobject:[[uibarbuttonitem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                         target:self
                                                                         action:nil]];
    
    UIBarButtonItem *title=[[uibarbuttonitem alloc] initWithTitle:@"My Test"
                                                            style:UIBarButtonItemStylePlain
                                                           target:self
                                                           action:nil];
    [toolbar setitems:[nsarray arraywithobject:title]];
    [toolbar setitems:toolbararray];
        [self.view addsubview:toolbar];
    
}
```