Farlanki.

ViewController转场

字数统计: 349阅读时长: 1 min
2016/04/23 Share

在看这篇文章之前,不妨先看看objccn的两篇文章
自定义 ViewController 容器转场
View Controller 转场

UIViewControllerAnimatedTransitioning

UIViewControllerAnimatedTransitioning这个协议要求实现两个方法:
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
第一个方法指定了转场动画的持续时间,第二个方法指定了转场动画.
还可以选择性的实现这个方法:
- (void)animationEnded:(BOOL)transitionCompleted;

UIViewControllerContextTransitioning

UIViewControllerContextTransitioning代表的是转场动画的上下文环境.
UIViewControllerContextTransitioning协议里的方法都要求实现.
其中,
- (UIViewController *)viewControllerForKey:(NSString *)key这个方法要求根据key返回controller.
key分别是UITransitionContextFromViewControllerKey,UITransitionContextToViewControllerKey.分别指定了将要消失的controller和将要出现的controller.

转场

如果要进行转场,调用实现了UIViewControllerAnimatedTransitioning协议的 - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;方法即可.
如果是tabBarController或者navigationController,只需要在相应的协议中实现(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC,返回一个实现UIViewControllerAnimatedTransitioning的类即可.
为什么两种方法不同呢,个人理解是在tabBarController或者navigationController中的转场,上下文可以由系统生成,所以不用自己实现UIViewControllerContextTransitioning了.

在转场动画完成后,要注意把消失的view的transform属性设置为CGAffineTransformIdentity(如果之前改变了的话).因为如果transform属性不为CGAffineTransformIdentity,在下次转场需要显示该view时,系统将不能计算其frame的值.

CATALOG
  1. 1. UIViewControllerAnimatedTransitioning
  2. 2. UIViewControllerContextTransitioning
  3. 3. 转场