0
votes

I need to present a view controller at the centre of another view controller with some animation effect. I want the transition is reusable, so I defined a a class to implement the UIViewControllerAnimatedTransitioning protocol. I just simply add constrains to the subView to locate it to the centre, change the color of the container, and perform the animation:

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
if (self.status == TransitioningStatusPresent) {
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];

    UIView *containerView = [transitionContext containerView];
    containerView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.25f];
    [containerView addSubview:toView];

    toView.translatesAutoresizingMaskIntoConstraints = NO;

    id c1 = [NSLayoutConstraint constraintWithItem:toView
                                         attribute:NSLayoutAttributeCenterX
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:containerView
                                         attribute:NSLayoutAttributeCenterX
                                        multiplier:1.0f constant:0.0f];

    id c2 = [NSLayoutConstraint constraintWithItem:toView
                                         attribute:NSLayoutAttributeCenterY
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:containerView
                                         attribute:NSLayoutAttributeCenterY
                                        multiplier:1.0f constant:0.0f];
    [containerView addConstraints:@[c1, c2]];

    toView.alpha = 0.0f;
    [UIView animateWithDuration:TRANSITION_DURATION animations:^{
        toView.alpha = 1.0f;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}else{
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];

    fromView.alpha = 1.0f;
    [UIView animateWithDuration:TRANSITION_DURATION animations:^{
        fromView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

}

the code works if animated:YES.

enter image description here

However, it doesn't work as what I expect when without animation:

    [self presentViewController:messageBoxViewController animated:NO completion:nil];

enter image description here

It simply because the function -animateTransition: will not be called when there is not animation. Therefore, I think I should not put the constrains in this function, but where should I put it to?

My App needs to be compatible to iOS 7, so presentation controller is not allow. But I need to access the container. So How could I present the view controller with custom transitioning with the -presentViewController:animated:completion: method.

So how do I solve the problem?

1

1 Answers

0
votes

Do not add constraints, try animating with frames. It will work. But if you make animated as NO then obviously it will never get executed. If you facing many problems then what you can do it is, take the snapshot of sourceController and add that to your destinationController and then animate your view to the center. Make sure your animated property is false.