自定义模态转换不起作用 - 示例

时间:2017-02-28 08:22:12

标签: ios iphone

我正在尝试让UIViewControllerContextTransitioning正常工作。

我想要的是什么:

我想要的是提供具有自定义动画和透明背景的模态视图控制器。

我做过什么:

我创建了一个实现UIViewControllerTransitioningDelegate的动画师,

为模态控制器设置:

self.modalPresentationStyle = UIModalPresentationCustom;
self.transitioningDelegate = self;

到目前为止我取得的成就

模态控制器动画正确显示和解除视图,但在解雇完成后,整个应用程序变黑。我使用xCode工具来选择窗口层次结构中的内容,并且没有任何内容。我的猜测是,当我添加到上下文的容器时,我改变了VC的超级视图。

动画

@implementation AlertAnimator

const static CGFloat kAnimationDuration = 1.2;

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIViewController *to = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *from = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    if (self.transitionType == ModalAnimatedTransitioningTypePresent) {
        [self animatePresentingInContext:transitionContext toVC:to fromVC:from];
    } else if (self.transitionType == ModalAnimatedTransitioningTypeDismiss) {
        [self animateDismissingInContext:transitionContext toVC:to fromVC:from];
    }
}

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

- (void)animatePresentingInContext:(id<UIViewControllerContextTransitioning>)transitionContext toVC:(UIViewController *)toVC fromVC:(UIViewController *)fromVC {
    CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC];
    CGRect toVCRect = fromVCRect;
    toVCRect.origin.y = toVCRect.size.height;

    toVC.view.frame = toVCRect;
    UIView *container = [transitionContext containerView];
    [container addSubview:fromVC.view];
    [container addSubview:toVC.view];

    [UIView animateWithDuration:kAnimationDuration animations:^{
        toVC.view.frame = fromVCRect;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

- (void)animateDismissingInContext:(id<UIViewControllerContextTransitioning>)transitionContext toVC:(UIViewController *)toVC fromVC:(UIViewController *)fromVC {
    CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC];
    fromVCRect.origin.y = fromVCRect.size.height;

    UIView *container = [transitionContext containerView];
    [container addSubview:toVC.view];
    [container addSubview:fromVC.view];

    [UIView animateWithDuration:kAnimationDuration animations:^{
        fromVC.view.frame = fromVCRect;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

@end

代码示例:

https://www.dropbox.com/s/oaghtgwvga4nxs4/Test.zip?dl=0

问题:

我做错了什么以及为什么屏幕会变黑?

1 个答案:

答案 0 :(得分:0)

如果这是一个呈现动画师,你不应该将fromVC.view作为子视图添加,因为它已经存在。这会导致它出错。试一试,告诉我发生了什么。如果是解雇动画师,你不应该将presentsViewController添加到层次结构中。