Question

I am using a custom segue to navigate between two view controllers. The custom segue uses a CGAffineTransformMakeScale to create a zoom effect on the destination controller.

When I run the project in portrait mode everything works ok, but when I change to landscape there is a problem. The destination controller appears on the screen with the portrait orientation, then it animates to the correct scale (1.0, 1.0) and when the animation is done it changes to the correct orientation. This is really confusing for me and I can't find anything online on this problem so any help will be greatly appreciated.

The project that I use to test this uses two view controllers each with a button which triggers the segue.

This is my CustomZoomSegue.m file:

#import "CustomZoomSegue.h"

@implementation CustomZoomSegue

- (void)perform
{
    UIViewController *destinationViewController = (UIViewController *)self.destinationViewController;
    UIViewController *sourceViewController = (UIViewController *)self.sourceViewController;
    UIWindow *mainWindow = [[UIApplication sharedApplication].windows objectAtIndex:0];

    UIView *sourceView = [sourceViewController view];
    UIView *destinationView = [destinationViewController view];

    destinationView.frame = sourceView.frame;
    [mainWindow addSubview:destinationView];

    [destinationView setAlpha:0.0f];
    [destinationView setTransform:CGAffineTransformMakeScale(0.0, 0.0)];

    // slide newView over oldView, then remove oldView
    [UIView animateWithDuration:0.8
                     animations:^{
                         [destinationView setTransform:CGAffineTransformMakeScale(1.0, 1.0)];
                         [destinationView setAlpha:1.0f];
                     }
                     completion:^(BOOL finished){
                         [destinationView removeFromSuperview];
                         [sourceViewController presentViewController:destinationViewController animated:NO completion:nil];
                     }];
}

@end

Does anyone have any idea why this happens? It is driving me crazy

Was it helpful?

Solution 2

I got this to work by adding the destination view to the source view as a subview. Before I was adding it directly to the main window.

Here is the code that manages the scale and zoom simultaneously in the animation block:

- (void)perform
{
    UIViewController *sourceViewController = (UIViewController *) self.sourceViewController;
    UIViewController *destinationViewController = (UIViewController *) self.destinationViewController;

    [sourceViewController.view addSubview:destinationViewController.view];
    [destinationViewController.view setFrame:sourceViewController.view.window.frame];

    [destinationViewController.view setBounds:sourceViewController.view.bounds];
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    [destinationViewController.view setCenter:CGPointMake(screenSize.width/2 + 127, screenSize.height/2 - 138)];

    [destinationViewController.view setTransform:CGAffineTransformMakeScale(0.0,0.0)];

    [UIView animateWithDuration:1.8
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         [destinationViewController.view setAlpha:0.0];
                         [destinationViewController.view setTransform:CGAffineTransformMakeScale(1.0,1.0)];
                         [destinationViewController.view setAlpha:0.8];
                     }
                     completion:^(BOOL finished){
                         [destinationViewController.view setAlpha:1.0];
                         [destinationViewController.view removeFromSuperview];
                         [sourceViewController presentViewController:destinationViewController animated:NO completion:nil];
                     }];
}

OTHER TIPS

I'd guess the problem might be related to animating the destination view before presenting the view controller. The controller is involved in view rotation, but it won't be asked to rotate until it's presented. I think you'll have better results if you present first and then use images of the source and destination views to do the animation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top