iosobjective-cuiinterfaceorientationinterface-orientationviewwilltransitiontosize

Handling Interface Orientation with Animation in iOS7


Since iOS8, there is the very convenient viewWillTransitionToSize method to handle interface orientation changes. I use this the following way in my app (for the sake of simplicity, I only use a single UIView, elementView, I want to resize in the example):

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{   
    // Define the new screen size we are about to transition to
    windowSize = size;

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context){
         UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
         // Animate UI element to new dimensions
        [self setUpUIElements];
    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context){ 
    // Housekeeping after animation (just so there is something here)
    randomBool = YES;
    }];
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

-(void) setUpUIElements {
    elementView.frame = CGRectMake(0.0f, 
                                   0.0f, 
                                   windowSize.size.width, 
                                   windowSize.size.height);
}

This method handles orientation changes beautifully, animating the resize method alongside the actual rotation; couldn't be any simpler. I, however, would like to support iOS 7 alongside iOS8 and iOS9, and this method is not available there. So, my question is:

How could I get the same smoothly animated resizing result with legacy, iOS7 code?

I was experimenting with methods like willRotateToInterfaceOrientation but was having trouble determining the exact dimensions of the new interface and how to animate my resizing methods alongside the device rotation. I need help from someone who is more familiar with legacy code.


Solution

  • Since there was no answer, I was forced to dig through old documentations and spend a few days trying out various solutions. Long story short: the below method do the job just done (I hope it will help people who want to support legacy devices):

    - (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
        // Typecast the interface orientation to which the app is transitioning
        UIDeviceOrientation orientation = (UIDeviceOrientation)toInterfaceOrientation;
    
        // Call method that handles the resizing
        [self handleInterfaceOrientation:orientation withDuration:duration];
    } // <- For leagcy iOS7 support
    
    - (void)handleInterfaceOrientation:(UIDeviceOrientation)toInterfaceOrientation withDuration:(NSTimeInterval)duration {
    
        // Calculate screen size the app is transitioning to (ignore iOS8-only devices, such as iPhone 6 and newer)
        if ( (toInterfaceOrientation == UIInterfaceOrientationPortrait) || (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) ) {
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
                if ([[UIScreen mainScreen] bounds].size.height > 480.0f) {
                    windowSize          = CGSizeMake(320.0f, 568.0f);
                } else {
                    windowSize          = CGSizeMake(320.0f, 480.0f);
            }
            } else {
                    windowSize          = CGSizeMake(768.0f, 1024.0f);
            }
        } else {
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
                if ([[UIScreen mainScreen] bounds].size.height > 480.0f) {
                    windowSize          = CGSizeMake(568.0f, 320.0f);
                } else {
                    windowSize          = CGSizeMake(480.0f, 320.0f);
                }
            } else {
                    windowSize          = CGSizeMake(1024.0f, 768.0f);
            }
        }
    
        // Animate UI elements to new dimension
        [UIView animateWithDuration:duration
                              delay:0.0f
                            options: UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             [self populateGalleryScrollView];
                         } completion:^(BOOL finished) {
                             // Once rotation is done, disable the rotation view
                            rotationView.alpha                      = 0.0f;
         }];
    } // <- For legacy iOS7 support