Monday, August 10, 2009

UIToolbar and Autorotate

Wait for it is over...


//==============================================================================
// Declare nCurrentOrientation at application level. Toolbar object also put at application level class
//==============================================================================
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
SampleAppDelegate *appDelegate = (SampleAppDelegate *)[[UIApplication sharedApplication] delegate];
CGRect mainViewBounds = [appDelegate.window bounds];

CGFloat toolbarHeight = 30;
switch (interfaceOrientation)
{
case UIInterfaceOrientationLandscapeLeft:
{
if (appDelegate.nCurrentOrientation != UIInterfaceOrientationPortrait)
return NO;
// size up the toolbar and set its frame

[appDelegate.toolbar setFrame:CGRectMake(CGRectGetWidth(mainViewBounds),
toolbarHeight,
CGRectGetHeight(mainViewBounds),
toolbarHeight)];
//calculate new center point
CGPoint center = CGPointMake(305.0, CGRectGetHeight(mainViewBounds) / 2);

// Set the center point of the view to the center point of the window's content area.
appDelegate.toolbar.center = center;

//Transform the toolbar according to new orientation
appDelegate.toolbar.transform = CGAffineTransformMakeRotation( 3 * M_PI / 2.0);
appDelegate.nCurrentOrientation = UIInterfaceOrientationLandscapeLeft;


}
break;

case UIInterfaceOrientationLandscapeRight:
{
if (appDelegate.nCurrentOrientation != UIInterfaceOrientationPortrait)
return NO;

// size up the toolbar and set its frame

[appDelegate.toolbar setFrame:CGRectMake(0,
CGRectGetHeight(mainViewBounds) - toolbarHeight,
CGRectGetHeight(mainViewBounds),
toolbarHeight)];
//calculate new center point
CGPoint center = CGPointMake(toolbarHeight/2, CGRectGetHeight(mainViewBounds) / 2);
// Set the center point of the view to the center point of the window's content area.
appDelegate.toolbar.center = center;

//Transform the toolbar according to new orientation
appDelegate.toolbar.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
appDelegate.nCurrentOrientation = UIInterfaceOrientationLandscapeRight;


}
break;
case UIInterfaceOrientationPortrait: //EXIF = 3
{
if ( appDelegate.nCurrentOrientation != UIInterfaceOrientationLandscapeRight && appDelegate.nCurrentOrientation != UIInterfaceOrientationLandscapeLeft)
return NO;

// size up the toolbar and set its frame

toolbarHeight = 42;
//calculate new center point
CGPoint center = CGPointMake(CGRectGetWidth(mainViewBounds)/2, toolbarHeight/2);
appDelegate.toolbar.center = center;
//Transform the toolbar according to new orientation
appDelegate.toolbar.transform= CGAffineTransformMakeRotation(2*M_PI );
[appDelegate.toolbar setFrame:CGRectMake(0,
CGRectGetHeight(mainViewBounds)-toolbarHeight,
CGRectGetWidth(mainViewBounds),
toolbarHeight)];

appDelegate.nCurrentOrientation = UIInterfaceOrientationPortrait;


}

default:
break;
}

return ( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

2 comments:

  1. Hello
    i used this code , working fine but i think its working if we rotate iPhone step by step means left then vertcle then right and viceaversa. Is it intended in this code by putting following line?

    if (appDelegate.nCurrentOrientation != UIInterfaceOrientationPortrait)
    return NO;


    Anyways thnx for sharing buddy.

    ReplyDelete
  2. Yes, this code handles rotation step by step. But you can change code . You only have to calculate the transformation correctly on all combination of rotation.

    ReplyDelete