iOS7苹果为开发者引入了一系列新的API。其中一个尤为出色的API是一个新的视图控制器转换API。有趣的是,对于这个API苹果大量使用了协议,而不是具体的对象。虽然有点奇怪,但这种定义方式为我们提供了极大的灵活性,在深入探讨这个API之前,让我们看一下另一个iOS7的新特性。
注意导航控制器在iOS7中默认行为的改变,在导航控制器不同视图切换之间的动画在iOS7之后现在略有不懂,它是可交互的,即我们俗称的右滑返回。
所谓转场动画就是视图切换动画,交互式就是动画切换的进度依赖于用户的操作。本文将围绕UINavigationController的push、pop操作切换控制器进行讨论。但,苹果所提供的API是同样支持控制器的present、dismiss操作切换控制器与UITabbarViewController切换控制器的,其实现原理基本一致,在次就不多赘述了,希望和我一样在探索进阶之路的,如果看到这篇博客后能对你起到帮助,能自己去实现present、dismiss(实现UIViewControllerTransitioningDelegate)与UITabbarViewController(实现UITabBarControllerDelegate)的转场切换。
废话不多说,直入主题,我们今天做一个非常简单的demo,首先是一个不可交互的转场动画,push为渐入效果,pop为从屏幕大小缩小为CGZero。
push、pop操作么,肯定要与navigation controller扯上点关系了,我们说了新的API是代理,让我们来一起看一下UINavigationControllerDelegate中的一个方法
navigationController
operation
fromViewController:(
UIViewController *)
fromVC
toViewController:(
UIViewController *)
toVC
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
官方描述:UIKit calls this method to obtain the timing information for your animations. The value you provide should be the same value that you use when configuring the animations in your animateTransition: method. UIKit uses the value to synchronize the actions of other objects that might be involved in the transition. For example, a navigation controller uses the value to synchronize changes to the navigation bar.
UIKit调用此方法来获得你的动画的时间信息。你配置的值应该与你在animatetransition:方法中设置的动画时间是一致的。UIKit使用该值来同步其他对象可能涉及过渡的行动。例如,导航控制器使用该值来对导航栏进行同步更改。简而言之,就是在这个方法里设置动画的时间
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
官方描述:
UIKit calls this method when presenting or dismissing a view controller. Use this method to configure the animations associated with your custom transition. You can use view-based animations or Core Animation to configure your animations.
看到这里聪明的你脑袋里肯定已经有思路了,我们将一个类声明为UINavigationController的代理,在其中实现
navigationController
operation
fromViewController:(
UIViewController *)
fromVC
toViewController:(
UIViewController *)
toVC
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface BWCustomPushAnimation : NSObject<UIViewControllerAnimatedTransitioning