UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)
iOS5中,UIViewController新添加了几个方法:
- (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);
- (void) removeFromParentViewController NS_AVAILABLE_IOS(5_0); - (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(5_0); - (void)willMoveToParentViewController:(UIViewController *)parent NS_AVAILABLE_IOS(5_0);
- (void)didMoveToParentViewController:(UIViewController *)parent NS_AVAILABLE_IOS(5_0);
代码示例:
代码结构:

其中,MainViewController添加为window的rootViewController,FirstViewController、SecondViewController、ThirdViewController就是三个简单的controller,设置不同的背景色即可,MainViewController的xib文件如下图:

MainViewController的viewDidLoad方法实现如下:
- (void)viewDidLoad
{
[super viewDidLoad]; FirstViewController * firstController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self addChildViewController:firstController]; SecondViewController * secondController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self addChildViewController:secondController]; ThirdViewController * thirdController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[self addChildViewController:thirdController]; [contentView addSubview:firstController.view];//用于显示其它controller的view
currentController = firstController;
}
三个button的响应方法如下:
- (IBAction)onButtonClick:(id)sender {
FirstViewController * firstController = self.childViewControllers[];
SecondViewController * secondController = self.childViewControllers[];
ThirdViewController * thirdController = self.childViewControllers[];
NSInteger tag = ((UIButton *) sender).tag;
if ((currentController == firstController && tag == ) || (currentController == secondController && tag == ) || (currentController == thirdController && tag == )) {
return;
}
UIViewController * oldController = currentController;
switch (tag) {
case :{
[self transitionFromViewController:currentController toViewController:firstController duration: options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{} completion:^(BOOL finished) {
if (finished) {
currentController = firstController;
}
else {
currentController = oldController;
}
}];
break;
}
case : {
[self transitionFromViewController:currentController toViewController:secondController duration: options:UIViewAnimationOptionTransitionFlipFromTop animations:^{} completion:^(BOOL finished) {
if (finished) {
currentController = secondController;
}
else {
currentController = oldController;
}
}];
break;
}
case :{
[self transitionFromViewController:currentController toViewController:thirdController duration: options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{} completion:^(BOOL finished) {
if (finished) {
currentController = thirdController;
}
else {
currentController = oldController;
}
}];
break;
}
default:
break;
}
}
效果图:

UIViewController新方法的使用(transitionFromViewController:toViewController:duration:options:animations:completion:)的更多相关文章
- iOS5中UIViewController的新方法
iOS5中UIViewController的新方法 前言 在苹果的 WWDC2011 大会视频的<Session 101 - What's New in Cocoa> 和<Sessi ...
- 【C#代码实战】群蚁算法理论与实践全攻略——旅行商等路径优化问题的新方法
若干年前读研的时候,学院有一个教授,专门做群蚁算法的,很厉害,偶尔了解了一点点.感觉也是生物智能的一个体现,和遗传算法.神经网络有异曲同工之妙.只不过当时没有实际需求学习,所以没去研究.最近有一个这样 ...
- 【Android】一种提高Android应用进程存活率新方法
[Android]一种提高Android应用进程存活率新方法 SkySeraph Jun. 19st 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph ...
- Execel(导出新方法):
#region 新方法 //var sbHtml = new StringBuilder(); //sbHtml.Append("<table border='1' cellspaci ...
- 交换ctrl和caps_loack的新方法
交换ctrl和caps_loack的新方法 Table of Contents 1 过程 1 过程 debian用了几年,由于emacs的关系,一直将右ctrl和caps_lock键交换,使用的是xm ...
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...
- 用javascript得到客户端IP的新方法
javascript得到客户端IP的新方法 很久以来,我都是经过http://fw.qq.com/ipaddress来得到客户端用户的IP,这个方法简单.快速.实用 . 我们调用它的写法是: < ...
- spring AOP Bean添加新方法
目的:为studentAdditionalDetails中添加Student的showDetails()和ExtraShowDetails()两个方法 spring 中AOP能够为现有的方法添加额外 ...
- 看es6 字符串新方法有感
'x'.repeat(3) // "xxx" 'hello'.repeat(2) // "hellohello" 'na'.repeat(0) // " ...
随机推荐
- JavaScript中数组Array.sort()排序方法详解
JavaScript中数组的sort()方法主要用于对数组的元素进行排序.其中,sort()方法有一个可选参数.但是,此参数必须是函数. 数组在调用sort()方法时,如果没有传参将按字母顺序(字符编 ...
- ASP.NET MVC4 MVC 当前上下文中不存在名称“Scripts”
Views目录下的web.config文件 <pages>下<namespaces>下 加入<add namespace="System.Web.Optimiz ...
- 洛谷——P1469 找筷子
P1469 找筷子 题目描述 经过一段时间的紧张筹备,电脑小组的“RP餐厅”终于开业了,这天,经理LXC接到了一个定餐大单,可把大家乐坏了!员工们齐心协力按要求准备好了套餐正准备派送时,突然碰到一个棘 ...
- objective-c 强弱引用、properties的学习
一.强弱引用 强引用:strong reference 弱引用:weak reference 引用可以理解为指针A指向的对象B.换句话说,拥有指针A的对象是对象B的所有者(ownership). 区别 ...
- 深入JS正则先行断言
这里是 Mastering Lookahead and Lookbehind 文章的简单翻译,这篇文章是在自己搜索问题的时候stackoverflow上回答问题的人推荐的,看完觉得写得很不错.这里的简 ...
- HTML基础一
正所谓温故而知新, 近期有了总结知识点的想法, 想把之前学习的知识作一个分阶段性的总结, 就从html开始吧!!! 一.浏览器与服务器 1. 浏览器 1.1 浏览器是什么 1.浏览器是一种交互软件(与 ...
- 【Linux】CentOS7 上使用yum安装和卸载软件【yum安装wine举例】
关于yum的相关解释,请 man yum 自行查看. 配置常用源:http://www.cnblogs.com/sxdcgaq8080/p/7516186.html yum的使用类似于在windows ...
- 死磕 Fragment 的生命周期
死磕 Fragment 的生命周期 本文原创,转载请注明出处.欢迎关注我的 简书 ,关注我的专题 Android Class 我会长期坚持为大家收录简书上高质量的 Android 相关博文.本篇文章已 ...
- Linux文件压缩与解压命令
1 .zip 格式压缩与解压 压缩命令 zip 压缩文件名 源文件 zip -r 压缩目录名 源目录 解压命令 unzip 文件名 td@td-Lenovo-IdeaPad-Y41 ...
- 高效重构 C++ 代码
引言 Martin Fowler的<重构:改善既有代码的设计>一书从2003年问世至今已有十几年时间了,按照计算机领域日新月异的变化速度,重构已经算是一门陈旧的技术了.但是陈旧并不代表不重 ...