简体   繁体   中英

iOS 7 custom transition issues

I am trying to implement some custom transitions on the iPad but I am getting some issues. Since the x, y coordinates do not always start on the upper left corner in the containerView of the transitionContext when the device is rotated I wrote the following methods

- (CGRect)rectForPresentedStateStart:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *containerView = [transitionContext containerView];

    switch (fromViewController.interfaceOrientation)
    {
        case UIInterfaceOrientationLandscapeRight:
            return CGRectMake(0, containerView.bounds.size.height,
                              containerView.bounds.size.width, containerView.bounds.size.height * 2 / 3);
        case UIInterfaceOrientationLandscapeLeft:
            return CGRectMake(0, - containerView.bounds.size.height * 2 / 3,
                              containerView.bounds.size.height, containerView.bounds.size.height * 2 / 3);
        case UIInterfaceOrientationPortraitUpsideDown:
            return CGRectMake(- containerView.bounds.size.width * 2 / 3, 0,
                              containerView.bounds.size.width * 2 / 3, containerView.bounds.size.height);
        case UIInterfaceOrientationPortrait:
            return CGRectMake(containerView.bounds.size.width, 0,
                              containerView.bounds.size.width * 2 / 3, containerView.bounds.size.height);
        default:
            return CGRectZero;
    }

}

- (CGRect)rectForPresentedStateEnd:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];


    switch (toViewController.interfaceOrientation)
    {
        case UIInterfaceOrientationLandscapeRight:
            return CGRectOffset([self rectForPresentedStateStart:transitionContext], 0, - containerView.bounds.size.height * 2 / 3);
        case UIInterfaceOrientationLandscapeLeft:
            return CGRectOffset([self rectForPresentedStateStart:transitionContext], 0, containerView.bounds.size.height * 2 / 3);
        case UIInterfaceOrientationPortraitUpsideDown:
            return CGRectOffset([self rectForPresentedStateStart:transitionContext], containerView.bounds.size.width * 2 / 3, 0);
        case UIInterfaceOrientationPortrait:
            return CGRectOffset([self rectForPresentedStateStart:transitionContext], - containerView.bounds.size.width * 2 / 3, 0);
        default:
            return CGRectZero;
    }
}

On portrait mode and upside down everything seems to work fine. The issue is on landscape.

  1. When the devices orientation is UIInterfaceOrientationLandscapeLeft the view disappears for a second just before the transition starts.

  2. When the devices orientation is UIInterfaceOrientationLandscapeRight I get the following issue: The navigation bar is smaller and turns to normal when the transition ends like in the pictures

转换发生时导航栏出现问题

Issue with navigation bar when transition occurs

过渡完成后

After transition is completed

I am not sure if these are bugs from apple or if I am doing something wrong and if so how can I fix the issues?

It is definitely an apple bug. For presenting the animation I am using something like this:

if (self.presenting) {
        toViewController.view.frame = [self rectForPresentedStateStart:transitionContext];
        [transitionContext.containerView addSubview:toViewController.view];
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            fromViewController.view.frame = [self rectForDismissedState:transitionContext];
            toViewController.view.frame = [self rectForPresentedStateEnd:transitionContext];
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];    
}

The problem with the navigation controller is fixed if I add the view ( [transitionContext.containerView addSubview:toViewController.view]; ) after the animateWithDuration method. Still cannot figure out why.

I guess, your containerView is UIWindow . So it's bounds is orientation independent so containerView.bounds.size.height is the same in portrait and landscape.

If I'm right you need to use width as height and height as width in landscape

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM