ios 视图切换翻页效果
本文写的是视图切换,涉及到的内容有
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像素;
- - (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];
- }
[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];
按钮时间再次也不做过多解释,全部写在注释里了,其他几个都一样,只是修改了动画效果,变化不大,详情可下载源代码研究一下;
- -(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];
- }
附上代码:http://download.csdn.net/detail/duxinfeng2010/4407717
咱在这在研究一个问题,在RootAppDelegate.m中我们先看看系统生成的代码
- - (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要区别开,一个工程建立的,一个是系统本身自动生成的
- - (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函数先添加几行代码,以示区别
- - (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];
- }
然后,当运行程序的时候,加载的就是FirstViewController的视图了
ios 视图切换翻页效果的更多相关文章
- Activity切换动画(overridePendingTransition)-翻页效果
		Activity的切换动画指的是从一个activity跳转到另外一个activity时的动画.{它包括两个部分:一部分是第一个activity退出时的动画:另外一部分时第二个activity进入时的动 ... 
- iOS:横向使用iPhone默认的翻页效果
		大致思路使用两层辅助UIView的旋转来实现添加后的View的横向翻页效果 CATransform3D transformA = CATransform3DRotate(CATransform3DId ... 
- 关于Page翻页效果, PageViewConrtoller
		Page View Controllers你使用一个page view controller用page by page的方式来展示内容.一个page view controller管理一个self-c ... 
- Android 实现书籍翻页效果----升级篇
		自从之前发布了<Android 实现书籍翻页效果----完结篇 >之后,收到了很多朋友给我留言,前段时间由于事情较多,博客写得太匆忙很多细节地方没有描述清楚.所以不少人对其中的地方有不少不 ... 
- CSS3-----图片翻页效果的展示
		在开发一个网站的过程中各种翻页效果数不胜数,在这里我将介绍一下简单的一种方法就是使用css3的旋转即可实现这种常见的效果: 显示效果如下: 首先是页面的布局,不用那么复杂; a标签的href属性,一般 ... 
- ViewPager实现滑动翻页效果
		实现ViewPager的滑动翻页效果可以使用ViewPager的setPageTransformer方法,如下: import android.content.Context; import andr ... 
- Android平台中的三种翻页效果机器实现原理
		本文给开发者集中展现了Android平台中的三种翻页效果机器实现原理,希望能够对开发者有实际的帮助价值! 第一种翻页效果如下: 实现原理: 当前手指触摸点为a,则 a点坐标为(ax,ay), ... 
- 移动端ios上下滑动翻页事件失效
		移动端开发过程中,在添加上下滑动事件时候,引入了最常用的移动端库zepto.js及其touch模块,有一种现象,安卓的手机没有问题,上下滑动翻页很正常 :但是到了ios上面,好啊,上下滑动会出现弹性滚 ... 
- webapp应用--模拟电子书翻页效果
		前言: 现在移动互联网发展火热,手机上网的用户越来越多,甚至大有超过pc访问的趋势.所以,用web程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ... 
随机推荐
- ThinkPHP开发笔记-前后端数据交互
			此处就是 Controller 和 View 相互传数据. 1.Controller 向 View 的页面传数据.在控制器中把变量传递给模板,使用 assign 方法对模板变量赋值.例如: 在Cont ... 
- vs 2017 保存文件 utf8
			vs 2017 保存文件 utf8 转自:https://blog.csdn.net/jiegemena/article/details/79369650 
- 剑指offer算法总结
			剑指offer算法学习总结 节选剑指offer比较经典和巧妙的一些题目,以便复习使用.一部分题目给出了完整代码,一部分题目比较简单直接给出思路.但是不保证我说的思路都是正确的,个人对算法也不是特别在行 ... 
- 第六天 文件的基本管理和xfs文进系统备份恢复
			1.1 Linux系统目录结构,相对路径/绝对路径 1.1.1 Linux系统目录结构 在linux系统中一切都是文件 / 根目录,一切的起点,就像是一个树杈一样,他是所有叉的根 /bin 在单用户模 ... 
- Vim代码缩进设置
			前段配置VPS,无奈只能使用Vim编辑Python代码,比较头疼的没法设置自动缩进,所以搜索了相关的配置,特记录如下. 将以下的设置加入到~/etc/vim/.vimrc中: set sw=4 set ... 
- Dir命令
			注: 此系列为自己之前所搭建网站内容. 其实python的os模块能够很好的完成此任务.改天总结下. 之前在处理气象数据时,十几个文件,文件名比较长,需要自己处理变动的年份找出地址的规律再进行文件的读 ... 
- linux下使用FreeRDP 连接 Windows 远程桌面
			linux下使用FreeRDP 连接 Windows 远程桌面 简介 FreeRDP 是一款开源的远程桌面系统,支持多种平台, 在 ubuntu 中使用 FreeRDP 可以很方便的登录到 win ... 
- echarts 折线图配置
			html内容: <div id="user_num_chart" style="width: 582px;height:250px;"></d ... 
- debug调试日志和数据查询
			手动删除es文件并释放磁盘空间 1.停掉服务 systemctl stop xsdaemon.service 2.删掉索引 rm -rf /home/storager/c3dceb5e-bacc-4a ... 
- C++进阶3.字节对齐 联合
			C++进阶3.字节对齐 联合 20131011 多益和金山笔试 知识漏洞 20131011 前言: 今天下午是多益网络的笔试,整体感觉还好,但是找到很多的知识漏洞.一直笔试到6:00,然后紧张的从会生 ... 
