【翻译】自定义 UIViewController Transitions
iOS7中引入了控制器间的切换切换动画,适用于UINavigationController栈内核modal显示
iOS7 介绍了两种切换方式,一种是自动切换,另一种是交互式切换。下面就介绍一下NavigationController中实现fade动画切换。
源码地址:下载源码
Navigation Controller Delegate
在动画的世界里面充满了协议,然而现在这个项目中创建一个我们想要看见的视图,添加的协议需要交互式切换和模态视图的展现。
UINavigationControllerDelegate协议提供了4个新方法,用来决定自定义动画的切换。
我们感兴趣的方法是:
- (id<UIViewControllerAnimatedTransitioning>)navigationController:
animationControllerForOperation:
fromViewController:
toViewController:
这个方法将会在导航控制器切换过度的时候调用(同样适用于storyboard适用segue的过度),所以我们可以决定返回哪种类型的切换。
我们创建一个类作为NavigationController的delegate
@interface SCNavControllerDelegate : NSObject <UINavigationControllerDelegate>
@end
实现方法是:
@implementation SCNavControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
animationControllerForOperation:(UINavigationControllerOperation)operation
fromViewController:(UIViewController *)fromVC
toViewController:(UIViewController *)toVC
{
return [SCFadeTransition new];
}
@end
我们想要所有的切换都是相同的,不管是进入还是返回,所以我们返回的是SCFadeTransition对象给每一次切换。
设置代理是非常见到你的,你可以在项目里面看见:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self) {
_navDelegate = [SCNavControllerDelegate new];
self.delegate = _navDelegate;
}
return self;
}
_navDelegate 是一个ivar类型id<UINavigationControllerDelegate>.
创建自定义变换
我们看见这个代理需要返回一些切换对象,也就是返回一个遵循UIViewControllerAnimatedTransitioning协议的对象,这个协议中有三个方法,其中两个是必须要实现的。
transitionDuration:(必须实现)。返回动画的持续时间
animateTransition:(必须实现)在控制器间实现动画的过度。提供一个我们需要的对象用来联系不同的组件。
animationEnded:(选择实现)这个会在动画完成之后调用,可以在动画完成之后触发一些方法。
我们定义一个SCFadeTransition类来实现这两个方法
@interface SCFadeTransition : NSObject <UIViewControllerAnimatedTransitioning>
@end
实现方法如下
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 2.0;
}
当animateTransition:方法调用的时候提供一个遵循UIViewControllerContextTransitioning协议的对象,这个对象用来获取动画完成的点点滴滴。第一个方法我们用 viewControllerForKey:,让我们能够掌握两个视图控制器间的过度。
// Get the two view controllers
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
这个内容提供给我们一个UIView的动画,通过containerView方法联系
// Get the container view - where the animation has to happen
UIView *containerView = [transitionContext containerView];
我们必须联系每一个视图控制器的子视图
// Add the two VC views to the container
[containerView addSubview:fromVC.view];
[containerView addSubview:toVC.view];
我们不想看见过度的视图,所以我们必须把alpha属性设置为0:
toVC.view.alpha = 0.0;
现在我们已经提供了一个动画,在控制器间切换有个简单的fade效果,我们可以用UIView animation block:
[UIView animateWithDuration:[self transitionDuration:transitionContext]
delay:
options:
animations:^{
toVC.view.alpha = .f;
}
completion:^(BOOL finished) {
// Let's get rid of the old VC view
[fromVC.view removeFromSuperview];
// And then we need to tell the context that we're done
[transitionContext completeTransition:YES];
}];
知识点:
1、设置一个动画的持续时间,实现transitionDuration
2、"from"视图控制器的视图需要removed从view的层级中,当动画完成的时候。
3、completeTransition:方法在切换内容需要的时候调用,当动画完成的时候。



【翻译】自定义 UIViewController Transitions的更多相关文章
- 自定义UIViewController与xib文件关系深入分析
6月14日 上海 OSC 源创会开始报名啦,有很多机械键盘送哦!!! 用xcode模板向工程加入UIViewController sub class的时候,如果选中了with xib for inte ...
- Wordpress Polylang 翻译自定义格式
WordPress 多语言插件 Polylang 主题函数参考 重要:使用一个函数之前,你必须检查函数是否存在,否则,你的网站可能会在 Polylang 更新之前遇到致命错误(因为 WordPress ...
- 【翻译】Tusdotnet中文文档(3)自定义功能和相关技术
自定义功能和相关技术 本篇按照如下结构翻译 自定义功能 自定义数据仓库 相关技术 架构和总体概念 自定义数据仓库 tusdotnet附带一个存储库TusDiskStore,它将文件保存在磁盘上的一个目 ...
- iOS学习笔记-自定义过渡动画
代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...
- 8. UIViewController
1. UIViewController 的认识 UIViewController在iOS开发中占据很重要的位置,iOS的整个UI开发的核心思想也是MVC的架构,从UIViewController的命名 ...
- UIViewController简述
一.View Controller Classes 二.自定义UIVIewController 1.创建 a)nib文件 [cpp] view plaincopyprint? - (BOOL)ap ...
- iOS 7 新特性:视图控制器切换API
本文转载至 http://blog.jobbole.com/51588/ 本文由 伯乐在线 - studentdeng 翻译自 Chris Eidhof.欢迎加入技术翻译小组.转载请参见文章末尾处的要 ...
- [转]iOS应用性能调优的25个建议和技巧
写在前面 本文来自iOS Tutorial Team 的 Marcelo Fabri,他是Movile的一名 iOS 程序员.这是他的个人网站:http://www.marcelofabri.com/ ...
- iOS应用性能调优的25个建议和技巧【转】
转载自:http://blog.jobbole.com/37984/ 首页 最新文章 资讯 程序员 设计 IT技术 创业 在国外 营销 趣文 特别分享 更多 > - Navigation - ...
随机推荐
- Linux动态库生成以及调用
Linux下动态库文件的文件名形如 libxxx.so,其中so是 Shared Object 的缩写,即可以共享的目标文件. 在链接动态库生成可执行文件时,并不会把动态库的代码复制到执行文件中,而是 ...
- shell 灵活设置定时任务
#!/bin/bash step=30 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do curl #调用链接 sleep $step ...
- 关于hadoop: command not found的问题
问题: 昨天在安装完hadoop伪分布式之后,执行hadoop下的子项目--文字计数功能时出现该错误,然后今天执行 hadoop fs -ls命令时系统给出同样的错误提醒,经过查找资料,初步认为是ha ...
- 01-modal
Demo示例程序源代码
源代码下载链接:01-modal.zip37.8 KB // MJAppDelegate.h // // MJAppDelegate.h // 01-modal // // Created by ...
- [bzoj3224]Tyvj 1728 普通平衡树——splay模板
题目 你需要写一种数据结构支援以下操作. 插入元素. 删除元素. 查询元素的排名. 查询第k小的元素. 查询元素前趋. 查询元素后继. 题解 BBST裸题. 代码 #include <cstdi ...
- CVE-2016-6662 利用条件
首先执行SET GLOBAL 需要超级用户权限,所以利用条件要么用户本身是超级用户要么用户有trigger权限,通过创建trigger,由超级用户触发SET GLOBAL. 然而MYsql有个通过fi ...
- 【bzoj3744】GTY的妹子序列
大力分块+树状数组+主席树…… #include<bits/stdc++.h> #define N 50005 #define pa pair<int,int> #define ...
- 厦门大学XMUNET+ ubuntu连接方法
编辑连接 输入用户名和密码就好了 ubuntu16.04
- 编译opencv有关cuda的代码
opencv3.2提供了cuda很好的支持,cuda的opencv接口,让用户想使用opencv那样去使用cuda,不用写cuda代码 一开始编译opencv有关cuda的代码,opencv 里sam ...
- opencv3.0 legacy和nonfree丢失
根据官方http://docs.opencv.org/3.1.0/db/dfa/tutorial_transition_guide.html链接可以知道 legacy和nofree模块已经被删除了