UIViewControllerAnimatedTransitioning中的animateTransition不总是可以看到动画

时间:2014-07-31 14:03:54

标签: ios objective-c uinavigationcontroller

我正在使用UIViewController中包含的一些UINavigationController

segue上的UIButton推送storyboard,然后使用轻扫手势解除

popViewControllerAnimated

我使用UINavigationControllerDelegate提供符合​​UINavigationControllerDelegate的自定义对象。 animateTransition的代码如下所示。

我的问题是,第一次运行时,视图会在呈现时动画显示,但每次在此之后,它都没有动画效果(它只会立即显示)。

有人可以帮忙吗?

谢谢!

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {

//Get references to the view hierarchy
UIView *containerView = [transitionContext containerView];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];


if (self.isPresenting) {

    [containerView insertSubview:toViewController.view belowSubview:fromViewController.view];

    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        fromViewController.view.transform = CGAffineTransformMakeTranslation(-1000, 0);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];

} else if (!self.isPresenting) {
    //Add 'to' view to the hierarchy
    [containerView insertSubview:toViewController.view belowSubview:fromViewController.view];

    //Scale the 'from' view down until it disappears
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        //toViewController.view.transform = CGAffineTransformMakeScale(1.0, 1.0);
        fromViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}
}

2 个答案:

答案 0 :(得分:2)

升级到iOS8后,我遇到了类似的问题。要检查的一些事项:

  1. 确保在开始动画之前调用这些:

    [toViewController beginAppearanceTransition:YES animated:YES]; [fromViewController beginAppearanceTransition:NO animated:YES];

  2. 然后在完成块中调用它们:

    [toViewController endAppearanceTransition]; [fromViewController endAppearanceTransition]; [transitionContext completeTransition:finished];

  3. 上面需要确保viewWillAppear:和viewWillDisappear:在正确的时间被调用。

    1. 设置modalPresentationStyle = UIModalPresentationFullScreen,而不是UIModalPresentationCustom(行为方式非常不同)。
    2. 希望这有帮助。

      干杯!

答案 1 :(得分:1)

小心设置self.navigationController.delegate

你可能把它放在只运行一次的地方,比如 ViewDidLoad
尝试将其置于 ViewDidAppear

    override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)
        self.navigationController.delegate = self
    }