本文写的是视图切换,涉及到的内容有

1.实现代码添加Navigation Bar  Toolbal;

2.实现在Navigation Bar和Toolbar上用代码添加Bar Button Item;

3.UIView层面的简单动画效果

先把实现结果功能截图贴出来,对应动画效果

开始界面 和第一次向上翻页

向上翻页 和向下翻页

从左向右翻页 和从右向左翻页

开始制作:

1.创建一个新工程叫NVDemo; File->New->Project ->single View Application -> next

2.在新建两个ViewController,分别为FirstViewController和SecondViewController,顺便把XIB一块生成好

3.首先在视图上添加导航栏和导航按钮,经测试导航栏上只能添加两个导航按钮,和设置一个title标题;

我们还需要知道的一个常识是NavigationBar  ToolBar  Tab  Bar  都是44像素,所以在设置他们宽度时候他们的高度设置成44

还有一个通知栏,显示电量信息信号的地方是20像素;

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view, typically from a nib.
  5. //    创建导航栏,44像素
  6. UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  7. //    需要在导航栏上创建按钮,所以先创建一个导航栏集合
  8. UINavigationItem *navagationItem = [[UINavigationItem alloc] initWithTitle:@"导航栏"];
  9. UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(pageDown:)];
  10. UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"右测试"
  11. style:UIBarButtonItemStyleDone
  12. target:self
  13. action:@selector(leftpage:)];
  14. [navigationBar pushNavigationItem:navagationItem animated:YES];
  15. [navagationItem setLeftBarButtonItem:leftButton animated:YES];
  16. [navagationItem setRightBarButtonItem:rightButton animated:YES];
  17. [self.view addSubview:navigationBar];
  18. UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
  19. NSMutableArray *toolBarArray = [NSMutableArray array];
  20. [toolBarArray addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl
  21. target:self
  22. action:@selector(switchLoadView:)]];
  23. [toolBarArray addObject:[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
  24. target:self
  25. action:@selector(rightpage:)]];
  26. [toolBarArray addObject:[[UIBarButtonItem alloc]initWithTitle:@"MyTitle"
  27. style:UIBarButtonItemStylePlain
  28. target:self
  29. action:nil]];
  30. //UIToolBar上添加图像
  31. [toolBarArray addObject:[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"myImage.png"]
  32. style:UIBarButtonItemStylePlain
  33. target:self
  34. action:nil]];
  35. [toolBar setItems:toolBarArray animated:YES];
  36. [self.view addSubview:toolBar];
  37. }

[navigationBar pushNavigationItem:navagationItem animated:YES];涉及到一个压栈的操作,把navigationItem压到navigationBar里,导航栏上只能添加左右两个按钮;所以是setLeftBarButtonItem 和 setRightBarButtonItem,最后再将navigationBar添加到视图之上;

在Toolbar上添加可以添加各种按钮,创建一个可变数组,把添加的按钮全部放到数组上,[toolBar setItems:toolBarArrayanimated:YES];将数组里按钮集合添加到了toolBar上面,选取图片的时候素材没选好,所以显示出来的图片那个按钮效果不是太好

4.接下来说的是按钮事件,因为需要用到FirstViewController和SecondViewController,在RootViewController.m添加上他们的头文件,为了区别确实是两个视图的切换,在他们的ViewDidLoad函数中初始化视图的背景颜色,

self.view.backgroundColor = [UIColor yellowColor];  和self.view.backgroundColor = [UIColor redColor];

按钮时间再次也不做过多解释,全部写在注释里了,其他几个都一样,只是修改了动画效果,变化不大,详情可下载源代码研究一下;

  1. -(void) switchLoadView:(id)sender
  2. {
  3. //开始一个动画
  4. [UIView beginAnimations:@"Curl" context:nil];
  5. //    设置动画方式,开始和结束时动画效果比较慢
  6. [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
  7. //    动画持续时间
  8. [UIView setAnimationDuration:1.25];
  9. //    设置动画效果,向上翻页
  10. [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
  11. //    首先判断firstView视图父视图是否为空
  12. if (firstView.view.superview == nil)
  13. {
  14. //        父视图为空,在判断他的子视图是否为空,如果为空在创建一个视图加载上面
  15. if (firstView.view == nil) {
  16. FirstViewController *firstViewDemo = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
  17. firstView = firstViewDemo;
  18. }
  19. //        把seconView视图从父视图中移除
  20. [secondView.view removeFromSuperview];
  21. //        在当前视图插入子视图firstView的视图
  22. [self.view insertSubview:firstView.view atIndex:0];
  23. }
  24. else {
  25. if (secondView.view == nil)
  26. {
  27. SecondViewController *secondViewDemo = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
  28. secondView = secondViewDemo;
  29. }
  30. [firstView.view removeFromSuperview];
  31. [self.view insertSubview:secondView.view atIndex:0];
  32. }
  33. //       动画结束
  34. [UIView commitAnimations];
  35. }

附上代码:http://download.csdn.net/detail/duxinfeng2010/4407717

咱在这在研究一个问题,在RootAppDelegate.m中我们先看看系统生成的代码

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. // Override point for customization after application launch.
  5. self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
  6. self.window.rootViewController = self.viewController;
  7. [self.window makeKeyAndVisible];
  8. return YES;
  9. }

系统直接加载的就是的RootViewController的视图,也就是弹出第一个界面是RootViewController.xib,假如说我想第一个就显示的FirstViewController控制的视图怎么办? 我们就可以在这个函数中进行重写

在delegateApp.h中@class RootViewController后面添加@class FirstViewController;此处声明一个类,在这样当我们添加@property (strong, nonatomic) FirstViewController *fTestViewController;才不会报错;

只需修改两行代码,此处注释原先加载RootViewController视图的代码,可能是我命名的不够合理,RootViewController和rootViewController要区别开,一个工程建立的,一个是系统本身自动生成的

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. // Override point for customization after application launch.
  5. //    self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
  6. //    self.window.rootViewController = self.viewController;
  7. self.fTestViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
  8. //  把根控制视图设置成fTestViewController
  9. self.window.rootViewController = self.fTestViewController;
  10. [self.window makeKeyAndVisible];
  11. return YES;
  12. }

然后再到我们的FirstViewController.m的ViewDidLoad函数先添加几行代码,以示区别

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. // Do any additional setup after loading the view from its nib.
  5. self.view.backgroundColor = [UIColor yellowColor];
  6. UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
  7. NSMutableArray *toolBarArray = [NSMutableArray array];
  8. [toolBarArray addObject:[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction
  9. target:self
  10. action:nil]];
  11. UIBarButtonItem *title=[[UIBarButtonItem alloc] initWithTitle:@"My Test"
  12. style:UIBarButtonItemStylePlain
  13. target:self
  14. action:nil];
  15. [toolBar setItems:[NSArray arrayWithObject:title]];
  16. [toolBar setItems:toolBarArray];
  17. [self.view addSubview:toolBar];
  18. }

然后,当运行程序的时候,加载的就是FirstViewController的视图了

ios 视图切换翻页效果的更多相关文章

  1. Activity切换动画(overridePendingTransition)-翻页效果

    Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画.{它包括两个部分:一部分是第一个activity退出时的动画:另外一部分时第二个activity进入时的动 ...

  2. iOS:横向使用iPhone默认的翻页效果

    大致思路使用两层辅助UIView的旋转来实现添加后的View的横向翻页效果 CATransform3D transformA = CATransform3DRotate(CATransform3DId ...

  3. 关于Page翻页效果, PageViewConrtoller

    Page View Controllers你使用一个page view controller用page by page的方式来展示内容.一个page view controller管理一个self-c ...

  4. Android 实现书籍翻页效果----升级篇

    自从之前发布了<Android 实现书籍翻页效果----完结篇 >之后,收到了很多朋友给我留言,前段时间由于事情较多,博客写得太匆忙很多细节地方没有描述清楚.所以不少人对其中的地方有不少不 ...

  5. CSS3-----图片翻页效果的展示

    在开发一个网站的过程中各种翻页效果数不胜数,在这里我将介绍一下简单的一种方法就是使用css3的旋转即可实现这种常见的效果: 显示效果如下: 首先是页面的布局,不用那么复杂; a标签的href属性,一般 ...

  6. ViewPager实现滑动翻页效果

    实现ViewPager的滑动翻页效果可以使用ViewPager的setPageTransformer方法,如下: import android.content.Context; import andr ...

  7. Android平台中的三种翻页效果机器实现原理

    本文给开发者集中展现了Android平台中的三种翻页效果机器实现原理,希望能够对开发者有实际的帮助价值! 第一种翻页效果如下:     实现原理: 当前手指触摸点为a,则 a点坐标为(ax,ay), ...

  8. 移动端ios上下滑动翻页事件失效

    移动端开发过程中,在添加上下滑动事件时候,引入了最常用的移动端库zepto.js及其touch模块,有一种现象,安卓的手机没有问题,上下滑动翻页很正常 :但是到了ios上面,好啊,上下滑动会出现弹性滚 ...

  9. webapp应用--模拟电子书翻页效果

    前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...

随机推荐

  1. MySQL索引失效的场景

    WHERE字句的查询条件里有不等于号(WHERE column!=-),MYSQL将无法使用索引 类似地,如果WHERE字句的查询条件里使用了函数(如:WHERE DAY(column)=-),MYS ...

  2. JSP 标准标签库(JSTL)

    JSP 标准标签库(JSTL) JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能. JSTL支持通用的.结构化的任务,比如迭代,条件判断,XML文档操作,国际化标签, ...

  3. vue结合element-ui 的select 全选问题

    下拉列表多选 问题 通过操作 所有来进行全选 全不选问题 element-ui 中 select 记录下自己最近使用element-ui 中的 select多选问题 在element中默认是指单纯多选 ...

  4. opengl:初次接触

    1.概述 OpenGL(Open Graphics Library),开放的图形程序接口,是编程接口的规范,并不是像OpenCV那样是库.GLFW是开源的基于opengl标准的库,并且是跨平台的.其开 ...

  5. 在js中做数字字符串补0

    转自(http://blog.csdn.net/aimingoo/article/details/4492592) 通常遇到的一个问题是日期的“1976-02-03 HH:mm:ss”这种格式 ,我的 ...

  6. rsync技巧

    rsync格式: # 拷贝本地文件.当SRC和DES路径信息都不包含有单个冒号”:”分隔符时就启动这种工作模式.如:rsync -a /data /backup rsync [OPTION]... S ...

  7. kmplayer加速播放视频(转)

    转自微博:http://blog.sina.com.cn/shaguazhu1213 KMPlayer控制播放速度的快捷方式 (2011-11-12 10:51:56) 标签: 杂谈 分类: 编程之旅 ...

  8. 备注下Windows可能会用到的运行命令

    因为有几个命令不常用忘记了,所以备注下Windows可能会用到的运行命令: 1.cleanmgr:打开磁盘清理工具2.compmgmt.msc:计算机管理3.charmap:启动字符映射表4.calc ...

  9. Alpha冲刺(2/10)

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/9960487.html 作业博客:https://edu.cnblogs.com/campus/ ...

  10. IR Cut Filter

    IR cut filter,即红外截止滤光片,它放在于LENS与Sensor之间.因人眼与CMOS Sensor对各波长的响应不同,人眼看不到红外光但sensor会感应,因此需要IR cut filt ...