Question

I have a custom segue where I am trying to do the reverse of the standard "Cover Vertical" segue transition. I think this should work:

UIView *srcView = ((UIViewController *)self.sourceViewController).view;
UIView *dstView = ((UIViewController *)self.destinationViewController).view;

[dstView setFrame:CGRectMake(0, 0, srcView.window.bounds.size.width, srcView.window.bounds.size.height)];

[srcView.window insertSubview:dstView belowSubview:srcView];

[UIView animateWithDuration:0.3
     animations:^{
         srcView.center = CGPointMake(srcView.center.x - srcView.frame.size.width, srcView.center.y);
     }
     completion:^(BOOL finished){
         [srcView removeFromSuperview];
     }
 ];

The problem is the destination view shows up in portrait orientation even though every other view in the app is landscape orientation. Also, the x and y coordinates are reversed. Why is this happening?

I have set up shouldAutorotateToInterfaceOrientation in every view controller, but that didn't help. I could rotate the view with CGAffineTransformMakeRotation, but that doesn't seem like the right thing to do; coordinates would still be reversed, for one thing.

Update:

I tried using CGAffineTransformRotate to rotate the view , but it looks ridiculous. Most of the background shows up black. I don't think this is the way it's suppose to work.

Was it helpful?

Solution 3

Apparently, it was much easier than I thought to do the segue I wanted. Here's is what worked:

- (void)perform {
    UIViewController *src = (UIViewController *)self.sourceViewController;
    [src dismissViewControllerAnimated:YES completion:^(void){ [src.view removeFromSuperview]; }];
}

Hopefully this helps someone else.

OTHER TIPS

Since I was not able the get the correct orientation and framing of the destination view before the controllers exchange, I did reverse the process:

  1. save the source view as an image (see Remove custom UIStoryboardSegue with custom animation ) ;
  2. switch the current controller [presentViewController: ...];
  3. then, do the animation with the (now correct) destination view embedding the saved source image.

Here is the complete code :

#include <QuartzCore/QuartzCore.h>

@implementation HorizontalSwipingSegue

- (void) perform {
    UIViewController *source = self.sourceViewController;
    UIViewController *destination = self.destinationViewController;    
    UIView *sourceView = source.view;
    UIView *destinationView = destination.view;

    // Create a UIImageView with the contents of the source
    UIGraphicsBeginImageContext(sourceView.bounds.size);
    [sourceView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *sourceImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView *sourceImageView = [[UIImageView alloc] initWithImage:sourceImage];

    [[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:NULL];

    CGPoint destinationViewFinalCenter = CGPointMake(destinationView.center.x, destinationView.center.y);
    CGFloat deltaX = destinationView.frame.size.width;
    CGFloat deltaY = destinationView.frame.size.height;

    switch (((UIViewController *)self.sourceViewController).interfaceOrientation) {
       case UIDeviceOrientationPortrait:
            destinationView.center = CGPointMake(destinationView.center.x - deltaX, destinationView.center.y);                
            sourceImageView.center = CGPointMake(sourceImageView.center.x + deltaX, sourceImageView.center.y);
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            destinationView.center = CGPointMake(destinationView.center.x + deltaX, destinationView.center.y);
            sourceImageView.center = CGPointMake(sourceImageView.center.x + deltaX, sourceImageView.center.y);
            break;
        case UIDeviceOrientationLandscapeLeft:
            destinationView.center = CGPointMake(destinationView.center.x, destinationView.center.y - deltaY);                
            sourceImageView.center = CGPointMake(sourceImageView.center.x + deltaY, sourceImageView.center.y);
            break;
        case UIDeviceOrientationLandscapeRight:
            destinationView.center = CGPointMake(destinationView.center.x, destinationView.center.y + deltaY);
            sourceImageView.center = CGPointMake(sourceImageView.center.x + deltaY, sourceImageView.center.y);
            break;
    }

    [destinationView addSubview:sourceImageView];

    [UIView animateWithDuration:0.6f
                     animations:^{
                         destinationView.center = destinationViewFinalCenter; 
                     }
                     completion:^(BOOL finished){
                         [sourceImageView removeFromSuperview];
                     }];
}

@end

UPDATED ANSWER:

Make sure you are setting your dstView's frame correctly. I think you'll have better luck using:

dstView.frame = srcView.frame;

instead of:

[dstView setFrame:CGRectMake(0, 0, srcView.window.bounds.size.width, srcView.window.bounds.size.height)];

the bounds property does not react to changes in orientation the same way frame does.

Replace

[srcView insertSubview:dstView belowSubview:srcView];

with

[srcView insertSubview:dstView atIndex:0];

If you are just adding another view controller's view to your view hierarchy, that view controller is not going to get any information about the orientation of the device, so it will present the view in its default configuration. You need to add the view controller into the view controller hierarchy as well, using the containment methods described here. You also need to remove it from the hierarchy when done.

There are also several good WWDC talks on the subject, one from 2011 called "Implementing view controller containment" and one from this year, the evolution of view controllers.

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