• 【翻译】自定义 UIViewController Transitions


      原文地址:http://www.shinobicontrols.com/blog/posts/2013/10/03/ios7-day-by-day-day-10-custom-uiviewcontroller-transitions

      iOS7中引入了控制器间的切换切换动画,适用于UINavigationController栈内核modal显示

      iOS7 介绍了两种切换方式,一种是自动切换,另一种是交互式切换。下面就介绍一下NavigationController中实现fade动画切换。

      源码地址:下载源码

     

    Navigation Controller Delegate

      在动画的世界里面充满了协议,然而现在这个项目中创建一个我们想要看见的视图,添加的协议需要交互式切换和模态视图的展现。

      UINavigationControllerDelegate协议提供了4个新方法,用来决定自定义动画的切换。

      我们感兴趣的方法是:

      

    - (id<UIViewControllerAnimatedTransitioning>)navigationController:
                                      animationControllerForOperation:
                                                   fromViewController:
                                                     toViewController:

    这个方法将会在导航控制器切换过度的时候调用(同样适用于storyboard适用segue的过度),所以我们可以决定返回哪种类型的切换。

    我们创建一个类作为NavigationController的delegate

    @interface SCNavControllerDelegate : NSObject <UINavigationControllerDelegate>
    @end

    实现方法是:

    @implementation SCNavControllerDelegate
        - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                          animationControllerForOperation:(UINavigationControllerOperation)operation
                                                       fromViewController:(UIViewController *)fromVC
                                                         toViewController:(UIViewController *)toVC
        {
            return [SCFadeTransition new];
        }
    @end

    我们想要所有的切换都是相同的,不管是进入还是返回,所以我们返回的是SCFadeTransition对象给每一次切换。

    设置代理是非常见到你的,你可以在项目里面看见:

    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if(self) {
            _navDelegate = [SCNavControllerDelegate new];
            self.delegate = _navDelegate;
        }
        return self;
    }

     _navDelegate  是一个ivar类型id<UINavigationControllerDelegate>.

    创建自定义变换

    我们看见这个代理需要返回一些切换对象,也就是返回一个遵循UIViewControllerAnimatedTransitioning协议的对象,这个协议中有三个方法,其中两个是必须要实现的。

    transitionDuration:(必须实现)。返回动画的持续时间

    animateTransition:(必须实现)在控制器间实现动画的过度。提供一个我们需要的对象用来联系不同的组件。

    animationEnded:(选择实现)这个会在动画完成之后调用,可以在动画完成之后触发一些方法。

    我们定义一个SCFadeTransition类来实现这两个方法

    @interface SCFadeTransition : NSObject <UIViewControllerAnimatedTransitioning>
    @end

    实现方法如下

    - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
    {
        return 2.0;
    }

    animateTransition:方法调用的时候提供一个遵循UIViewControllerContextTransitioning协议的对象,这个对象用来获取动画完成的点点滴滴。第一个方法我们用 viewControllerForKey:,让我们能够掌握两个视图控制器间的过度。

    // Get the two view controllers
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    这个内容提供给我们一个UIView的动画,通过containerView方法联系

    // Get the container view - where the animation has to happen
    UIView *containerView = [transitionContext containerView];

    我们必须联系每一个视图控制器的子视图

    // Add the two VC views to the container
    [containerView addSubview:fromVC.view];
    [containerView addSubview:toVC.view];

    我们不想看见过度的视图,所以我们必须把alpha属性设置为0:

    toVC.view.alpha = 0.0;

    现在我们已经提供了一个动画,在控制器间切换有个简单的fade效果,我们可以用UIView animation block:

    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                          delay:0
                        options:0
                     animations:^{
                         toVC.view.alpha = 1.f;
                     }
                     completion:^(BOOL finished) {
                         // Let's get rid of the old VC view
                         [fromVC.view removeFromSuperview];
                         // And then we need to tell the context that we're done
                         [transitionContext completeTransition:YES];
                     }];

    知识点:

    1、设置一个动画的持续时间,实现transitionDuration

    2、"from"视图控制器的视图需要removed从view的层级中,当动画完成的时候。

    3、completeTransition:方法在切换内容需要的时候调用,当动画完成的时候。

     

  • 相关阅读:
    常见Oracle HINT的用法
    2011年的每一天是周几?
    TOM上关于JOIN跟+号的讨论
    关于index_ffs使用索引的一点问题.
    数据库中分组字符串相加
    国服3.3.5:死亡骑士全系DPS饰品收益评分
    WLK狂暴战怎么玩
    3.3萨满手册
    关于clob类型在函数中的处理。
    pivot_clause [Oracle SQL]
  • 原文地址:https://www.cnblogs.com/iOS-dd/p/3573231.html
Copyright © 2020-2023  润新知