调用finishInteractiveTransition后 - 黑屏

时间:2016-02-01 13:03:45

标签: ios objective-c uiviewcontroller uiviewanimationtransition

我正在尝试实施转换,例如iOS 8邮件应用程序可刷卡"新电子邮件"的viewController。这一切都很好,除了一件事 - 当"新电子邮件" vc(第二个vc)放在屏幕的底部 - 第一个viewController处于非活动状态。我的代码(没有包含animateTransition:):

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                   presentingController:(UIViewController *)presenting
                                                                   sourceController:(UIViewController *)source {
    return self;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return self;
}

- (id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator{
    return  nil;
}

- (id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator{
    return self;
}

#pragma mark - UIViewControllerInteractiveTransitioning

- (void)startInteractiveTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    self.transitionContext = transitionContext;
    [self updateInteractiveTransition:0.0];
}

#pragma mark - UIPercentDrivenInteractiveTransition

- (void)updateInteractiveTransition:(CGFloat)percentComplete{
    NSLog(@"updateInteractiveTransition:%f", percentComplete);

    if (percentComplete < 0) {
        percentComplete = 0;
    }
    else if (percentComplete > 1){
        percentComplete = 1;
    }

    UIViewController *modalVC = [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *nonModalVC = [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    float scaleFactor = 0.9 + 0.1 * percentComplete;
    float alphaVal = 0.5 + 0.5 * percentComplete;
    nonModalVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, scaleFactor, scaleFactor);
    nonModalVC.view.alpha = alphaVal;


    CGRect modalVCFrame = modalVC.view.frame;
    modalVCFrame.origin.y = percentComplete * viewH + kModalViewYOffset;
    modalVC.view.frame = modalVCFrame;
}

- (void)cancelInteractiveTransitionWithDuration:(CGFloat)duration{
    NSLog(@"cancelInteractiveTransitionWithDuration");


    UIViewController *modalVC = [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *nonModalVC = [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    CGRect modalVCFrame = modalVC.view.frame;
    modalVCFrame.origin.y = self.cancelUp ? kModalViewYOffset : viewH - kModalViewNavBarHeight;
    NSLog(@"modalVCFrame.origin.y = %f", modalVCFrame.origin.y);
    CGAffineTransform transformVal = self.cancelUp ?
                                                CGAffineTransformMakeScale(kNonModalViewMinScale, kNonModalViewMinScale)
                                                : CGAffineTransformIdentity;
    CGFloat alphaVal = self.cancelUp ? 0.5 : 1.0;

    [UIView animateWithDuration:duration animations:^{
        modalVC.view.frame = modalVCFrame;
        nonModalVC.view.transform = transformVal;
        nonModalVC.view.alpha = alphaVal;
    } completion:^(BOOL finished) {
        [self cancelInteractiveTransition];
    }];

    reversed = NO;
}


- (void)finishInteractiveTransitionWithDuration:(CGFloat)duration{

    UIViewController *modalVC = [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *nonModalVC = [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    CGRect modalVCFrame = modalVC.view.frame;
    modalVCFrame.origin.y = viewH ; //+ kModalViewNavBarHeight;

    [UIView animateWithDuration:duration animations:^{
        modalVC.view.frame = modalVCFrame;
        nonModalVC.view.transform = CGAffineTransformIdentity;
        nonModalVC.view.alpha = 1.0;
    } completion:^(BOOL finished) {

        [modalVC.view removeFromSuperview];
        [self.transitionContext completeTransition:YES];
        self.transitionContext = nil;

        [self finishInteractiveTransition];

    }];

    reversed = NO;
}

如果我像这样修改startInteractiveTransition方法

- (void)startInteractiveTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
     NSLog(@"startInteractiveTransition");

    self.transitionContext = transitionContext;

    UIViewController *modalVC = [self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *nonModalVC = [self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [self.transitionContext containerView];
    [containerView insertSubview:nonModalVC.view belowSubview:modalVC.view];

    [self updateInteractiveTransition:0.0];
} 

然后第一个viewController将被激活,当第二个放在底部时,但当我关闭secondVC时 - 出现黑屏。问题在于解雇我们无法向内容视图添加任何内容,因为原始演示者仍然存在并且将通过删除容器视图来显示。但是当secondVC在底部时,如何使firstVC响应?

1 个答案:

答案 0 :(得分:0)

解决了我的问题。首先 - 在解雇时不要添加到containerView。第二 - 当modalView放在底部时,使containerView“通过”触及自身。为此,我为UIView添加了类别并覆盖hitTest:withEvent:方法