Saturday, October 15, 2011

Control auto rotation only for 'Landscape' or 'Portrait' in iOS.



When other view controller is used both views. You want to use one view controller only use as 'Landscape' or 'Portrait'.

Call this 'setupOneRotation' method in viewWillAppear: (as [self setupOneRotation];)




BOOL            startWithLandscape;



#pragma mark autoRotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

- (void)setupOneRotation
{
    if (self.view.frame.size.width < self.view.frame.size.height) {
       
        nameOfAppDelegate* sharedApplication = (nameOfAppDelegate *)[[UIApplication  sharedApplication] delegate];       // More details for this, use this link
       
       
        startWithLandscape = YES;
       
        [UIView beginAnimations:@"View Flip" context:nil];
        [UIView setAnimationDuration: 0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       
        sharedApplication.window.transform = CGAffineTransformIdentity;
       
       
        UIInterfaceOrientation toInterfaceOrientation = self.interfaceOrientation; // get current interface orientation
        //

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait){
            sharedApplication.window.transform = CGAffineTransformMakeRotation(-M_PI/2);
           
            [[UIApplication sharedApplication]
             setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
        }else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            sharedApplication.window.transform = CGAffineTransformMakeRotation(M_PI/2);
           
            [[UIApplication sharedApplication]
             setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
           
        }
       
        sharedApplication.window.bounds = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
        sharedApplication.window.center  = CGPointMake (384.0, 512.0);

        [UIView commitAnimations];
    }
}

- (void) viewWillDisappear:(BOOL)animated
{
    if (startWithLandscape) {
       
       
        nameOfAppDelegate* sharedApplication = (nameOfAppDelegate *)[[UIApplication  sharedApplication] delegate];
        [UIView beginAnimations:@"View Flip" context:nil];
        [UIView setAnimationDuration: 0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       
        sharedApplication.window.transform = CGAffineTransformIdentity;
            //sharedApplication.window.transform = CGAffineTransformMakeRotation(M_PI/2);
        sharedApplication.window.bounds = CGRectMake(0.0f, 0.0f, 768.0f, 1024.0f);
        sharedApplication.window.center  = CGPointMake (384.0, 512.0);
       
        [UIView commitAnimations];
    }
   
    [super viewWillDisappear: NO];
}

No comments:

Post a Comment

Add your comment