iOS开发-自定义Navigation转场效果

作者: Huangbaoqin | 来源:发表于2017-06-26 11:44 被阅读300次
    • 创建实现了UINavigationControllerDelegate的对象
    • 实现动画体
    • 设置NavigationController的代理

    1、创建实现了UINavigationControllerDelegate的对象

    // CustonNavigationControllerDelegate.m
    - (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                                animationControllerForOperation:(UINavigationControllerOperation)operation
                                                             fromViewController:(UIViewController *)fromVC
                                                               toViewController:(UIViewController *)toVC {
        if (operation == UINavigationControllerOperationPop) {
            return _popAnimation;
        }
        if (operation == UINavigationControllerOperationPush) {
            return _pushAnimation;
        }
        return nil;
    }
    

    2、实现动画体

    @implementation PushTransitionAnimation
    
    - (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
        return 0.6;
    }
    
    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
        UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
        UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        
        [[transitionContext containerView] addSubview:toViewController.view];
        toViewController.view.alpha = 0;
        
        [UIView animateWithDuration:[self transitionDuration:transitionContext]
                         animations:^{
                             [fromViewController.view setTransform:CGAffineTransformMakeScale(0.1, 0.1)];
                             toViewController.view.alpha = 1.0;
                         }
                         completion:^(BOOL finished) {
                             [fromViewController.view setTransform:CGAffineTransformIdentity];
                             [transitionContext completeTransition:YES];
                         }];
    }
    
    @end
    

    3、设置NavigationController的代理

    // AppDelegate.m
    _navDelegate = [[CustonNavigationControllerDelegate alloc] init];
    homeNav.delegate = _navDelegate;
    

    Demo地址

    相关文章

      网友评论

        本文标题:iOS开发-自定义Navigation转场效果

        本文链接:https://www.haomeiwen.com/subject/usydcxtx.html