Swift: 是用Custom Segue还是用Transition动画
用一个很简单的场景做为例子:在storyboard上,你有用UINavigationController串起来两个UIViewController。这两个controller之间要互相跳转,A->B, B->A。跳转的时候默认的那个push来push去的效果你觉得很傻X,所以想换一个效果。比如,不那么二的fade in/out效果。
很多的例子会说写一个cusom的UIStoryboardSegue,然后在这个里面写一个UIView.animationWithDuration来实现这个效果。千万别这么干!从iOS7开始就有一个更加方便简洁的方法可以实现这个效果。
下面就开始介绍这个很牛X的方法。首先创建一个single view的项目。名称可以叫做transitionDemo。各个controller之间的关系是这样的:

一个UINavigationController作为启动controller,串起来一个UITableViewController(root controller)和一个UIViewController。在Prototype Cells上ctrl+drag到view controller上,并选择show。
下面分别为table view controller创建类TDTableViewController为view controller创建类TDViewController之后分别在storyboard里关联起来。把从table view controller到view controller的segue的identitifer设置为TDViewController。
接下来给TDTableViewController添加数据源:
class TDTableViewController: UITableViewController {
var tableViewDataSource: [String]?
override func viewDidLoad() {
super.viewDidLoad()
createDataSource()
}
func createDataSource() {
if let _ = self.tableViewDataSource {
return
}
self.tableViewDataSource = [String]()
for i in 0..<100 {
self.tableViewDataSource!.append("item :- [\(i)]")
}
}
//............
}
打开storyboard,在TDViewController里添加一个label,给这个label添加约束,随便是什么约束都可以只要是正确的。然后在controller里添加这个label的outlet并关联。
在TDViewController代码中添加数据属性,并在viewDidLoad方法里给这label的text赋值:
class TDViewController: UIViewController {
@IBOutlet weak var dataLabel: UILabel!
var singleData: String!
override func viewDidLoad() {
super.viewDidLoad()
self.dataLabel.text = self.singleData
}
}
使用默认的segue,这里现在是show模式,跳转。并从table view controller出传递数据给view controller:
// 使用segue跳转
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.dataItem = self.tableViewDataSource![indexPath.row]
self.performSegueWithIdentifier("TDViewController", sender: nil)
} // 传递数据
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier != "TDViewController" {
return
} let destinationController = segue.destinationViewController as! TDViewController
destinationController.singleData = self.dataItem!
}
run一把来看看:

Segue和Transition
custom segue
直接用代码把两种方式都实现出来,分别代替上面使用的默认的实现方式。
首先弄一个custom segue。要开发一个custom segue,首先需要创建一个类并继承自UIStoryboardSegue,我们为这个类命名为DetailStoryboardSegue。在这个类中关键就是实现方法perform()。
import UIKit
class DetailStoryboardSegue: UIStoryboardSegue {
override func perform() {
let sourceView = self.sourceViewController.view // 1
let destView = self.destinationViewController.view
let window = (UIApplication.sharedApplication().delegate as! AppDelegate).window
window?.insertSubview(destView, aboveSubview: sourceView) // 2
destView.alpha = 0.0
UIView.animateWithDuration(0.3, animations: { // 3
destView.alpha = 1.0
})
}
}
self.sourceViewController和self.destinationViewController都是UIStoryboardSegue类本身就有的属性。从A controller跳转到B controller,那么source就是A,destination就是B。我们的fade in/out效果就是通过source和destination controller的view的切换和alpha实现的。- 在window上做source和destination view的切换。把destination view覆盖到source view上。
- destination view的alpha在上一步设置为了0,也就是完全透明的。在动画中把destination view的alpha设置回完全不透明,把view呈现在用户面前达到fade in的效果。
实现完成后,在storyboard中把segue的Kind 设置为custom,然后给Class设置为类DetailStoryboardSegue。

其实很简单,运行起来看看。

你会看到,运行的结果出了一点问题。之前在正确位置显示的label,在这里居然出现在了屏幕的顶端。这是因为前面TDViewController是用navigation controller的push出来的,屏幕的最顶端有一个navigation bar,所以label的top约束是有效的。而我们的custom segue的切换中并不存在navigation controller的push。而是简单的view的覆盖替换,没有navigation bar。所以labe的top约束失效了,直接被显示在了顶端。
这个错误其实引出了一个问题,但是这里我们暂时不做深入讨论。先看看Transitioning animtion动画是怎么运行的,然后我们讨论这个严肃的问题。
custom transitioning animation
实现自定义的切换动画就需要实现UIViewControllerAnimatedTransitioning这个protocol了。我们自定义一个类DetailTransitioningAnimator来实现这个protocol。
这个protocol有两个方法是必须实现的,func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval和func animateTransition(transitionContext: UIViewControllerContextTransitioning)。来看代码:
import UIKit
class DetailTransitioningAnimator: NSObject, UIViewControllerAnimatedTransitioning {
let durationTimeInterval: NSTimeInterval
// 1
init(duration: NSTimeInterval){
durationTimeInterval = duration
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return self.durationTimeInterval
}
//2
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()
let sourceController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
let destController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let sourceView = sourceController!.view
let destView = destController!.view
containerView?.addSubview(destView) // 3
destView.alpha = 0.0
UIView.animateWithDuration(0.3, animations: {
destView.alpha = 1.0
}, completion: {completed in
let cancelled = transitionContext.transitionWasCancelled()
transitionContext.completeTransition(!cancelled)
})
}
func animationEnded(transitionCompleted: Bool) {
}
}
- 这个
init方法不是必需的,但是为了可以自定义动画的执行时间添加这个构造方法。 - transitioning动画的执行方法。在这个方法里实现我们在之前的custom segue里实现的效果。
- 注意这里,用的是
let containerView = transitionContext.containerView()得到的container view。而不是之前用到的window。
下面把transitioning动画应用到代码中。在使用的时候需要实现UINavigationControllerDelegate。创建一个类NavigationControllerDelegate来实现这个protocol。
import UIKit
class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate {
func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return DetailTransitioningAnimator(duration: 0.3)
}
}
这里没有太多需要讲的。只要知道是这个方法就可以了。
下面在storyboard中把类NavigationControllerDelegate应用在UINavigationController中。
- 把object这个东西拖动到项目中唯一存在的navigation controller上。

- 给刚刚的object设置custom class为类
NavigationControllerDelegate。
只要关注右侧的Class的设置就可以。
- 给navigation controller的代理设置为刚刚拖动上去的delegate。

对上面的custom segue代码去掉,并稍作修改之后,运行起来。

木有问题,上面出现的错误也没有了。
这个问题不重要,但是还是要讨论一下:为什么custom segue会出现问题,而transitoning动画就没有问题呢?这里是因为实际上transitioning动画是在navigation controller的基础上改变的。Transitioning动画只是修改了navigation controller的push动画,改成了fade in的效果,而navigation controller的其他机制没有改动。custom segue则完全是两个view之间的动画。那么,这里就留下一个问题由读者去修改上面的custom segue代码来让这个navigation bar 出现出来。
但是什么情况下用custom segue,什么情况下用transition动画呢?Transitioning动画更加的灵活,不像custom segue是在storyboard里定死的。你可以根据不同的情况设定你自己想要的动画。
custom segue就是用来调用一些如:presentViewController和dismissViewController之类的方法的。这些方法调用的时候两个view controller之间的转化动画则使用上面的方法来做。他们的职责是不同的。
最后补充一点。也是一个发挥custom segue的作用的地方。在多个storyboard的情况下可以使用custom segue。步骤:(假设你已经创建了另外一个storyboard,叫做Another.storyboard)
- 拖一个storyboard reference到你现在的storyboard中。
- 点选这个storyboard reference,并在右侧工具栏的Storyboard下填写另外一个storyboard的名字,这里是Another.storyboard。
- ctrl+drag,从一个view controller中的按钮等view连接到刚刚添加的storyboard reference上。(确保你的另外一个storyboard的view controller已经设置为默认启动)
- 如果你要启动的是另外一个storyboard的某一个特定的view controller,那么就可以写一个custom segue了。在这个custom segue中初始化出另外一个storyboard,从中获取到你要启动的view controller,然后在custom segue的perform方法中完成controller跳转的最后一步。
all code here
to be continued。。。
Swift: 是用Custom Segue还是用Transition动画的更多相关文章
- 移动端 transition动画函数的封装(仿Zepto)以及 requestAnimationFrame动画函数封装(仿jQuery)
移动端 css3 transition 动画 ,requestAnimationFrame 动画 对于性能的要求,h5优先考虑: 移动端 单页有时候 制作只用到简单的css3动画即可,我们封装一下, ...
- CSS3的transition动画功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- css3 Transition动画执行时有可能会出现闪烁的bug
css3 Transition动画执行时有可能会出现闪烁的bug,一般出现在开始的时候. 解决方法: 1.-webkit-backface-visibility: hidden; 2.-webkit- ...
- 解决transition动画与display冲突的几种方法
如demo(如果没有显示,请查看源地址http://jsfiddle.net/ihardcoder/HNduT/2/)所示,基本的效果是在点击“Translate”按钮后,蓝色区域透明度变为0,然后隐 ...
- appendChild与Transition动画
在createElement之后,直接把这个div append到body中,是不会触发css3 transition动画的 必须要让浏览器计算div的css属性后,然后再设置div的style,才会 ...
- iOS涂色涂鸦效果、Swift仿喜马拉雅FM、抽屉转场动画、拖拽头像、标签选择器等源码
iOS精选源码 LeeTagView 标签选择控件 为您的用户显示界面添加美观的加载视图 Swift4: 可拖动头像,增加物理属性 Swift版抽屉效果,自定义转场动画管理器 Swift 仿写喜马拉雅 ...
- Swift:超炫的View Controller切换动画
匿名社交应用Secret的开发者开发了一款叫做Ping的应用,用户可以他们感兴趣的话题的推送. Ping有一个很炫的东西,就是主界面和之间切换的动画做的非常的好.每次看到一个非常炫的动画,都不由得会想 ...
- css3 transition动画
CSS3: 一.transition: <property> <duration> <animation type> <delay> eg: .div{ ...
- safari渲染Transition动画不流畅问题
用css3的transition过渡来做页面动画的时候,发现在chrome和ff流畅,在safari 不流畅: 度娘找到了淘宝UED的一个类似解决方案,动画就流畅了. 测试环境: win7 32bit ...
随机推荐
- maven中导入包版本冲突的解决
导入struts包,在struts核心包的ognl包下存在javassist包: 然后再导入hibernate包,在hibernate核心下也存在javassist包: 这样便会存在冲突,ecplis ...
- CSS-calc 兼容写法
width: 90%;/*写给不支持calc()的浏览器*/ width:-moz-calc(100% - (10px + 5px) * 2); width:-webkit-calc(100% - ( ...
- jquery关于attr和prop的差异
转自:http://www.jb51.net/article/88068.htm 处理像checkbox,radio和select这样的元素时,经常会发现明明使用了attr设置了selected或ch ...
- mybatis入门--mapper代理方式开发
不使用代理开发 之前,我们说了如何搭建mybatis框架以及我们使用mybatis进行简单的增删改查.现在,我们一起来构建一个dao层的完整代码.并用@test来模拟service层对dao层进行一下 ...
- java基础之—类加载器
要了解类加载器先要了解类的加载 一.类的加载(类的加载概述) 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 1.加载 就是指将clas ...
- Loadrunner使用键盘快捷键
---------Loadrunner使用键盘快捷键------------ alt+f8 比较当前快照(仅限于 Web Vuser) alt+ins 新建步骤 ctrl+a 全选 ctrl+c 复制 ...
- eclipse集成svn进行项目开发
在用eclipse进行项目开发的时候,报了一个错误:switch不支持String的参数.这个问题的原因是因为jre版本低于1.7,而当前的eclipse版本最高只能选1.6,无奈,我只能考虑换ecl ...
- 英国BBC出的这套中国风海报,设计美哭了!
“中国风”在国际上已经不是“小众”了 之前分享过好莱坞电影的中国风海报 没想到国外的电视剧也看上了中国市场 没错就是英国BBC的最长寿科幻剧—— <神秘博士Doctor Who> 前段时间 ...
- JSP属性的四种保存范围(page request session application)
JSP提供了四种属性的保存范围,分别为page.request.session.application 其对应的类型分别为:PageContext.ServletRequest.HttpSession ...
- Servlet API
Servlet API的查询网址:通过Tomcat的官网链接找到 可见,Servlet API有4个packages javax.servlet // 包含定义Servlet和Servlet容器之间契 ...


