I got the solution for the question asked here, but I don't like how the code is written.
This part of the code rotates the screen if it's in landscape and sets the size to match the screen. I don't like the if
and the 7x else if
. Can you suggest something more readable? Something like (appOrientation - fromInterfaceOrientation) == 2
than angle (M_PI / 2.0)
?
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation appOrientation = [UIApplication sharedApplication].statusBarOrientation;
float width = self.view.bounds.size.width;
float height = self.view.bounds.size.height;
//NSLog(@"width %3.0f, height: %3.0f", width, height);
// UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
// UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
// UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
// UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
CGFloat angle = 0;
// rotate from Portait
if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) && (appOrientation == UIInterfaceOrientationLandscapeLeft)){
angle = -(M_PI / 2.0);
}
else if((fromInterfaceOrientation == UIInterfaceOrientationPortrait) && (appOrientation == UIInterfaceOrientationLandscapeRight)){
angle = (M_PI / 2.0);
}
// rotate from PortraitUpsideDown
else if((fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) && (appOrientation == UIInterfaceOrientationLandscapeLeft)){
angle = (M_PI / 2.0);
}
else if((fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) && (appOrientation == UIInterfaceOrientationLandscapeRight)){
angle = -(M_PI / 2.0);
}
//rotate from Landscape:
else if((fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) && (appOrientation == UIInterfaceOrientationPortrait)){
// search for exactly the inverse...
}
// must be something more elgant code
CGAffineTransform transform = self.view.transform;
transform = CGAffineTransformRotate(transform, angle);
self.view.transform = transform;
[self.view setBounds:CGRectMake(0, 0, height, width)];