天天看点

iOS —— 转场

Modal presentation

  1. 设定ViewController的modalTransitionStyle属性。

    这种方式也对应Storyboard中对应的segue transition的设置。这个属性是一个枚举类型,其值代表已经定制的几种转场风格。这种方式也是最简单的转场,不带有任何自定义的转场效果代码。

  2. 使用UIView的animation API实现自定义的动画。

这种方式是比较常见实现方式。除了官方的文档以外,大量的Blog文章都会详细讲解这些API的用法。UIView的animation API的使用比较直观,相对来说也是一种比较容易学习的动画实现方式。

Navigation View Controller presentation

  1. 使用UIView的animation API。

    与Modal presentation相同,尽管在Storyboard中有专门为Navigation View Controller定制的Push segue(iOS8中被Show segue取代,因为后者支持iOS8中引进的Adaptive AutoLayout),但是Push segue并没有transtion属性,所以如果需要定制转场效果,可以使用UIVIew,使用方法和Modal presentation相同。

  2. 使用CATransition类。

    CATransition看起来更像为Navigation View Controller和TabBar View Controller这样的容器Controller定制的转场效果类。提供了很多内置的的动画效果。CATransition还可以结合CoreImage的滤镜CIFilter共同实现很炫的场景转换。若想详细了解CATransition的用法,可以读一读苹果的文档。

我们注意到,转场往往发生在流程切换的时候。所以上面的转场效果代码,往往会放在自定义的转场方法中(多见于使用Nib开发)或放在自定义的UIStoryboardSegue类中(多见于使用Storyboard开发)。所以很多时候,我们往往会碰到原生的转场方法与定制的动画效果有一定冲突。因为像presentViewController:animated:completion:这类方法本身就自带有内置的动画效果,自定义的动画效果往往在这个方法之外。所以很多时候需要用一些trick来避免这些问题。因此代码的可读性往往不会很好,并且写的不好的时候还会带来效率方面的问题。

iOS7以后,苹果引进了新的Transition API。这些API的使用方式,苹果没有给出一个官方的Guide,但是在网上,已经有很多Blog和教程讲解如何使用这些API,比如这篇文章。

新的Transition API完全改变了上面提到的动画与原生转场接口不兼容的问题。在新的API中,我们可以将动画效果代码单独封装到animator对象中,在设定好View Controller的transitoningDelegate后,再调用原生的转场方法,就会自动使用定制的动画效果。考虑到现在大部分App已经逐渐放弃了对iOS6的支持,所以这种方法是目前推荐的转场效果定制方法。单独封装的动画效果类在代码管理上也更加方便。

这里需要注意的一点,iOS6中引入的Storyboard Unwind Segue往往都需要一个Container View Controller。一个很常见的问题就是新手在定制Segue的时候往往会发生自定义的Unwind Segue不起作用。这个问题一般都是由于没有正确实现Container View Controller所需的方法带来的。

Storyboard中的转场

自iOS5引入Storyboard之后,iOS开发者在除了原有的Nib开发的基础上又有了新的方式来组织自己的UI和流程。Storyboard相对于传统的Nib,能够更加清晰的体现业务的流程,因此很受开发者欢迎。如今,很多教程都以Storyboard开发方式来讲解。而Storyboard中的Segue则是对转场流程的进一步封装。这个概念在Storyboard中至关重要,也是实现自定义转场的关键角色。

自定义Segue

自定义Segue的方式很简单,只要创建一个UIStoryboardSegue子类,并实现其perform方法即可。一个简单的实现如下:

- (void)perform
{
// Modal presentation segue
UIViewController *fromController = self.sourceViewController;
UIViewController *toController = self.destinationViewController;
[fromController presentViewController:toController animated:YES completion:^{
// Completion code here
}];
}
           

或者

- (void)perform
{
// Navigation ViewController segue(Push segue, Show segue in iOS8).
UIViewController *fromController = self.sourceViewController;
UIViewController *toController = self.destinationViewController;
[fromController.navigationController pushViewController:toController animated:YES];
}
           

自定义Unwind Segue

自定义Unwind Segue的方式与上面几乎完全一样,只不过调用的接口由presentViewController:animated:completion:和pushViewController:animated:换成dismissViewControllerAnimated:completion:和popToViewController:animated:。

但是Unwind Segue与普通的Segue有一个很大的不同,就是Unwind Segue的调用通常是由一个Container View Controller完成的。在iOS SDK的UIKit框架中,Navigation View Controller和TabBar View Controller都是常用的Container View Controller。

那么为什么Unwind Segue需要一个Container View Controllerl的支持?

这里就需要提一下Unwind Segue的设计初衷及其工作方式。之所以引入Unwind Segue,是为了应付任意跳转的情况,即从任意一个View Controller转场到特定的View Controller。在Nib的时代,这种工作往往通过delegate来完成。但是有了Unwind Segue以后,我们只要在需要跳转到的这个特定的View Controller类中实现一个签名为- (IBAction)unwindMethod:(UIStoryboardSegue *)segue这样的方法即可(其中unwindMethod可以替换为任何你喜欢的名称,但注意,当存在多个这样的方法时,名称不要相同,以免发生冲突,造成不可预料的后果)。这样,我们就可以在任意的View Controller(除了含有这个方法本身的View Controller)通过连接Segue来实现任意View Controller跳转到当前View Controller。不用再多写一行代码,这些都可以通过Interface Builder搞定,非常方便。

Unwind Segue的工作原理大致如下:

● 当我们通过UI事件或手动调用performSegueWithIdentifier:sender:方法触发一个Unwind Segue以后,首先UIKit会发送canPerformUnwindSegueAction:fromViewController:withSender:消息到sourceViewController询问是否处理UnwindSegue的action,由于sourceViewController不能处理(Unwind到自身没有意义),会返回NO

● UIKit然后会寻找sourceViewController的父Controller。如果sourceController是嵌入Navigation View Controller的子Controller,那么父Controller就是其navigationController

● 之后UIKit会发送viewControllerForUnwindSegueAction:fromViewController:withSender:消息给navigationController,询问能否找到一个负责处理此action的子Controller

● 在navigationController的默认viewControllerForUnwindSegueAction:fromViewController:withSender:实现中,navigationController会向自己的navigation栈上的所有子Controller发送

● canPerformUnwindSegueAction:fromViewController:withSender:消息。UIViewController类中,该方法的默认实现会查看unwinde segue action定义是否存在(即上面提到的特定签名的方法是否存在,这个方法的内部实现可以留空),若存在就返回YES。

● 如果navigationController的viewControllerForUnwindSegueAction:fromViewController:withSender:方法返回nil,则不会触发任何Unwind Segue

● 如果navgationController找到一个子类可以处理Unwind Segue的action,那么UIKit会发送segueForUnwindingToViewController:fromViewController:identifier:消息给navigationController,此方法将返回一个实际执行定制转场的segue实例

● 调用sourceViewController上的prepareForSegue:sender:方法

● 调用由viewControllerForUnwindSegueAction:fromViewController:withSender:方法返回的destinationViewController中的Segue action方法

● 调用Unwind Segue实例中的perform方法

从上面的我们可以知道,Unwind Segue的正常工作必须要有一个Container View Controller作为所有流程View Controller的父Controller来管理整个流程。在上面的原理说明中,这个父Controller是Navigation View Controller。如果我们要实现一个自己的定义的Container,就必须给自定义的View Controller类实现一些上面提到过的方法:

● canPerformUnwindSegueAction:fromViewController:withSender:

● viewControllerForUnwindSegueAction:fromViewController:withSender:

● segueForUnwindingToViewController:fromViewController:identifier:

关于这些方法的说明和实现方式,下面就来详细讨论一下。

自定义Container

实现自定义的Container View Controller

我们一般会在子Controller中通过实现canPerformUnwindSegueAction:fromViewController:withSender:来决定要不要执行相应的Unwind Segue。

在自定义的容器中,我们必须实现viewControllerForUnwindSegueAction:fromViewController:withSender:和segueForUnwindingToViewController:fromViewController:identifier:方法。前一个方法用来决定那个View Controller来处理Unwind Segue action,后一个方法用来返回自定义的Unwind Segue实例。

使用Modal presentation时需要注意的情况

当我们使用UIViewController的presentViewController:animated:completion:方法以Modal presentation的方式来跳转场景的时候,情况与在Navigation View Controller有很大不同。首先,使用这种方式跳转场景的时候,跳转到的View Controller为Source View Controller的子Controller,而在Navigation View Controller中,所有的流程Controller基本上都是Navgation View Controller的子Controller,所以二者在View Controller的层次管理上有很多不同。因此实现Modal presentation风格的Segue的时候,动画的view不能搞错,必须对View Controller中的顶层View操作。一个参考实现如下(略掉动画效果代码,仅提供转场方法调用代码):

Segue部分:

- (UIView *)findTopMostViewForViewController:(UIViewController *)viewController
{
UIView *theView = viewController.view;
UIViewController *parentViewController = viewController.parentViewController;
while(parentViewController != nil)
{
theView = parentViewController.view;
parentViewController = parentViewController.parentViewController;
}
returntheView;
}
- (void)perform
{
UIViewController *source = self.sourceViewController;
UIViewController *destination = self.destinationViewController;
// Find the views that we will be animating. If the source or destination
// view controller sits inside a container view controller, then the view
// to animate will actually be that parent controller's view.
UIView *sourceView = [self findTopMostViewForViewController:source];
UIView *destinationView = [self findTopMostViewForViewController:destination];
[source presentViewController:destination animated:NO completion:^{
// completion code here
}];
}
           

Unwind Segue部分:

- (UIView *)findTopMostViewForViewController:(UIViewController *)viewController
{
UIView *theView = viewController.view;
UIViewController *parentViewController = viewController.parentViewController;
while(parentViewController != nil)
{
theView = parentViewController.view;
parentViewController = parentViewController.parentViewController;
}
returntheView;
}
- (void)perform
{
UIViewController *source = self.sourceViewController;
UIViewController *destination = self.destinationViewController;
// Find the views that we will be animating. If the source or destination
// view controller sits inside a container view controller, then the view
// to animate will actually be that parent controller's view.
UIView *sourceView = [self findTopMostViewForViewController:source];
UIView *destinationView = [self findTopMostViewForViewController:destination];
[source dismissViewControllerAnimated:NO completion:^{
// completion code here
}];
}
           

注意:Modal Presentation的Unwind Segue很难实现无Bug的任意跳转,因为UIViewController中,跟Container View Controller相关的方法的默认实现并不能很好的定位Container View Controller。而以正确的方式重写这些方法并不容易。所以如果有任意跳转的需求,我们可以尝试自己实现一个简单的Container View Controller。

使用AddChildViewController API实现自己的Container View Controller

我们偶尔会希望有一个跟Navigation View Controller差不多的容器,但是又不希望像Navigation View Controller那么笨重,且限制多多。我们知道Navigation View Controller在Interface Builder中,其Navigation Bar能容纳的元素样式并不丰富,尽管大多数时候,我们能够通过UIAppearance来定制一些样式,但我们希望定制能容纳更加丰富的元素的Navigation Bar,或者其他定制的导航界面的时候,希望能够实现一个类似的容器。我们当然可以模仿Navigation View Controller的公开API实现一个差不多的东西,如果我们要很方便的使用自定义Segue和任意跳转的Unwind Segue的话,还需要以特定的方式实现上面提到的一些方法。UIViewController的addChildViewController:方法同样可以做出类似的功能,而且相比Modal presentation,这种方式代码更加直观。因为使用这个API实现的容器,对子Controller的管理方式与Navigation View Controller类似。

容器的部分代码如下:

- (UIViewController *)viewControllerForUnwindSegueAction:(SEL)action fromViewController:(UIViewController *)fromViewController withSender:(id)sender
{
for(UIViewController *childController inself.childViewControllers) {
if([childController canPerformUnwindSegueAction:action fromViewController:fromViewController withSender:sender]) {
returnchildController;
}
}
returnnil;
}
- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
UIStoryboardSegue *unwindSegue = [[MyLeftToRightUnwindSegue alloc] initWithIdentifier:identifier source:fromViewController destination:toViewController];
returnunwindSegue;
}
           

Segue代码:

- (BOOL)controllerInStack:(UIViewController *)controller
{
UIViewController *fromController = self.sourceViewController;
UIViewController *containerController = fromController.parentViewController;
for(UIViewController *childController incontainerController.childViewControllers) {
if(childController == controller) {
returnYES;
}
}
returnNO;
}
- (void)perform
{
// A simple transition.
// New scene slides in from right and old scene slides out to left.
UIViewController *fromController = self.sourceViewController;
UIViewController *toController = self.destinationViewController;
UIViewController *parentController = fromController.parentViewController;
UIView *containerView = parentController.view;
[containerView addSubview:toController.view];
CGRect initialFromRect = fromController.view.frame;
CGRect initialToRect = CGRectOffset(initialFromRect, initialFromRect.size.width, 0);
CGRect finalFromRect = CGRectOffset(initialFromRect, -initialFromRect.size.width, 0);
CGRect finalToRect = initialFromRect;
toController.view.frame = initialToRect;
if(![self controllerInStack:toController]) {
// notify containment event.
[toController willMoveToParentViewController:parentController];
}
[UIView animateWithDuration:0.4f animations:^{
fromController.view.frame = finalFromRect;
toController.view.frame = finalToRect;
} completion:^(BOOL finished) {
if(![self controllerInStack:toController]) {
// Add new controller as a child controller to the container view controller
[parentController addChildViewController:toController];
// notify containment event.
[toController didMoveToParentViewController:toController];
}
[fromController.view removeFromSuperview];
}];
}
           

Unwind Segue代码:

- (void)perform
{
// A simple transition.
// New scene slides in from left and old scene slides out to right.
UIViewController *fromController = self.sourceViewController;
UIViewController *toController = self.destinationViewController;
UIViewController *parentController = fromController.parentViewController;
UIView *containerView = parentController.view;
[containerView addSubview:toController.view];
CGRect initialFromRect = fromController.view.frame;
CGRect initialToRect = CGRectOffset(initialFromRect, -initialFromRect.size.width, 0);
CGRect finalFromRect = CGRectOffset(initialFromRect, initialFromRect.size.width, 0);
CGRect finalToRect = initialFromRect;
toController.view.frame = initialToRect;
[UIView animateWithDuration:0.4f animations:^{
fromController.view.frame = finalFromRect;
toController.view.frame = finalToRect;
} completion:^(BOOL finished) {
[fromController.view removeFromSuperview];
}];
}
           

当我们定义的Container View中有需要置顶的元素(比如定制的导航条)时,可以将addSubView:方法换成insertSubView:atIndex:方法来调整子视图的层次。