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程序做出仿原生效果的移动应用,也变得越来越流行了.这种程序也就是我们常说的单页应用程序,它也有一个英文 ...
随机推荐
- 锁(3)-- DB锁
1 前言 数据库大并发操作要考虑死锁和锁的性能问题.看到网上大多语焉不详(尤其更新锁),所以这里做个简明解释,为下面描述方便,这里用T1代表一个数据库执行请求,T2代表另一个请求,也可以理解为T1为一 ...
- C# Memcached 缓存
之前做的功能,程序可能有不足之处,但还是要记录下 ICacheStrategy.cs文件 public interface ICacheStrategy { /// <summary> / ...
- web.config文件配置解决网站上传大文件限制
Asp.Net网站对上传文件的大小,请求执行的时间都做了限制,上传的文件如果超过限制或者执行上传时间超出, 文件上传都将失败. 因此,需要配置web.config来增加最大文件上传的大小和执行超时时间 ...
- Windows command line monitor
一个可以查看当然系统中进程参数的小工具 下载地址
- Java 里的异常(Exception)详解
作为一位初学者, 本屌也没有能力对异常谈得很深入. 只不过Java里关于Exception的东西实在是很多. 所以这篇文章很长就是了.. 一, 什么是java里的异常 由于java是c\c++ ...
- OpenCL将数组从内存copy到显存
本来想对上一篇博客做优化,优化效果不明显.但知识点还是要记一下. 初衷是想把上一篇博客中定义域的计算搬到CPU来计算,因为定义域的计算对于每一个kernel都是一样的,所以直接读取应该是可以进一步减小 ...
- 51NOD-1960-数学/贪心
1960 范德蒙矩阵 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 LYK最近在研究范德蒙矩阵与矩阵乘法,一个范德蒙矩阵的形式如下: 它想通过构 ...
- UVALive-5713 Qin Shi Huang's National Road System (次小生成树)
题目大意:有n个城市,要修一些路使得任意两个城市都能连通.但是有人答应可以不计成本的帮你修一条路,为了使成本最低,你要慎重选择修哪一条路.假设其余的道路长度为B,那条别人帮忙修的道路两端城市中的总人口 ...
- Java内存状况查看方法和分析工具
Java本身提供了多种丰富的方法和工具来帮助开发人员查看和分析GC及其JVM内存的状况,同时开源界也有一些工具用于查看和分析GC和JVM内存的状况. 通过这些分析,可以排查程序中内存泄露的问题及调优程 ...
- C++进阶1模板的使用
C++进阶1模板的使用 20131010 C++中,我们自己编程虽然不会怎么使用模板,包括函数模板和类模板,但是在大型的项目开发中函数模板和类模板是非常重要的.笔者在**面试的时候忙问道过这个问题,迷 ...