UIViewControllerContextTransitioning和UINavigationControllerDelegate

时间:2016-04-22 10:00:33

标签: ios objective-c iphone uinavigationcontroller uiviewanimationtransition

我的代码:

navigator.m

- (void)newPushPage:(UIViewController *)controller
{
    [self pushViewController:controller animated:YES];
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC
{
    if (operation == UINavigationControllerOperationPush || operation == UINavigationControllerOperationPop)
    {
        self.animator = [Animator new]; 
        return self.animator;
    }
    return nil;
}

animator.m

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

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    [[transitionContext containerView] addSubview:toViewController.view];
    toViewController.view.alpha = 0;

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        fromViewController.view.transform = CGAffineTransformMakeScale(0.1, 0.1);
        toViewController.view.alpha = 1;
    } completion:^(BOOL finished) {
        fromViewController.view.transform = CGAffineTransformIdentity;
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];

    }];

}

在pushPage和屏幕出现之后,出现了一个问题:隐藏在代码中的所有元素都可见,我可以看到元素在看到屏幕时是如何消失的。看起来不美观。有办法吗?

1 个答案:

答案 0 :(得分:1)

在animateTransition(id)transitionContext协议中,推送和弹出过渡必须单独处理,如下所示。

// 1.设置fromVC ..

UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
CGRect sourceRect = [transitionContext initialFrameForViewController:fromVC];
CGRect finalFrameForVC = [transitionContext finalFrameForViewController:toVC];

// 2.插入toVC视图。

   if(pushCondition) { 
   UIView *container = [transitionContext containerView];
    [container insertSubview:toVC.view aboveSubview:fromVC.view];
    toVC.view.alpha = 0.5;
    toVC.view =
} else if (popCondition ){
    UIView *container = [transitionContext containerView];
    toVC.view.frame = finalFrameForVC;
    toVC.view.alpha = 0.5;
    [container addSubview:toVC.view];
    [container sendSubviewToBack:toVC.view];
    UIView *snapShoot = [fromVC.view snapshotViewAfterScreenUpdates:false];
} 

// 3.执行动画。

    [UIView animateWithDuration:1.0
                          delay:0.0
         usingSpringWithDamping:1.0
          initialSpringVelocity:6.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{

                         //Setup the final parameters of views for push  
                         toVC.view // update final view frame
                         toVC.view.alpha = 1.0;
                        //Setup the final parameters of views for pop
                        snapShoot.frame = 
                     } completion:^(BOOL finished) {
                         //When the animation is completed call completeTransition with final push value
                        [snapShoot removeFromSuperview];
                         //When the animation is completed call completeTransition with final push value
                         toVC.view.alpha= 1.0;
                         [transitionContext completeTransition:YES];

                     }];

在amimator中创建enum / flag属性并将其设置在导航控制器委托中。

if (operation == UINavigationControllerOperationPush)
{
    self.animator = [Animator new]; 
    self.animator.pushPopAnimation = UINavigationControllerOperationPush;
    return self.animator;
}

请参阅上面代码中的snapshotViewAfterScreenUpdates:(BOOL)afterUpdates方法的apple文档。