自定义模态转场动画

2017-02-28  本文已影响0人  YourFoot

最近模仿潮汐,发现不会实现自定义转场动画,就学习了下,还是有点不是很清晰,写下感想反思

View Controller Transition(控制器过渡)

每个 View Controller 是一个 Scene,View Controller Transition 便是从一个 Scene 转换到另外一个 Scene.

model转场方式仅限于modalPresentationStyle属性为 UIModalPresentationFullScreen 或 UIModalPresentationCustom 这两种模式

如果是试图控制器A跳转至B

自定义跳转需要三个东西:

  1.转场代理
          A作为代理,遵守UIViewControllerTransitioningDelegate协议
  2.动画控制器(Animation Controller):
          负责添加视图以及执行动画.   可创建两个遵守UIViewControllerAnimatedTransitioning的对象C,D,实现A到B和B到A的动画效果
   3.转场环境
           由UIKit提供

对象A实现UIViewControllerTransitioningDelegate 的两个方法:

    - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController*)presented   presentingController:(UIViewController*)presenting sourceController ///返回对象C
     -(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController*)dismissed //返回对象D

对象C.D实现UIViewControllerAnimatedTransitioning 协议的两个方法:

    - (NSTimeInterval)transitionDuration:(id<UIViewControllerAnimatedTransitioning>)transitionContext

返回动画执行时间

    - (void)animateTransition:(id<UIViewControllerAnimatedTransitioning>)transitionContext

从A到B,或者从B到A的动画效果

例:对象C中主要代码(负责A到B):
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
// 1. Get controllers from transition context
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

// 2. Set init frame for toVC
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGRect finalFrame = [transitionContext finalFrameForViewController:toVC];
toVC.view.frame = CGRectOffset(finalFrame, 0, screenBounds.size.height);

// 3. Add toVC's view to containerView
UIView *containerView = [transitionContext containerView];
[containerView addSubview:toVC.view];

// 4. Do animate now
NSTimeInterval duration = [self transitionDuration:transitionContext];
[UIView animateWithDuration:duration
                      delay:0.0
     usingSpringWithDamping:0.6
      initialSpringVelocity:0.0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     toVC.view.frame = finalFrame;
                 } completion:^(BOOL finished) {
                     // 5. Tell context that we completed.
                     [transitionContext completeTransition:YES];
                 }];
    }

这是我学习的demo:https://github.com/onevcat/VCTransitionDemo
更详细的可参见http://blog.devtang.com/2016/03/13/iOS-transition-guide/

上一篇下一篇

猜你喜欢

热点阅读