I am currently creating an iPhone app using Xcode 4.0.2, I want the app to switch views when the user turns their phone. For example: If a user turns their iPhone on it’s side, the iPhone would change to a pre-made landscape view. I have created the code for it, but when I run it in iOS Simulator and rotate the device nothing happens. What am I doing wrong?
Header File:
#import "FlipsideViewController.h"
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
IBOutlet UIView *portraitView;
IBOutlet UIView *landscapeView;
}
@property (nonatomic, retain) UIView *portraitView;
@property (nonatomic, retain) UIView *landscapeView;
@end
Implementation File (.m):
#import "MainViewController.h"
#define deg2rad (3.1415926/180.0)
@implementation MainViewController
@synthesize portraitView;
@synthesize landscapeView;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.view=landscapeView;
self.view.transform=CGAffineTransformMakeRotation(deg2rad*(90));
self.view.bounds=CGRectMake(0.0, 0.0, 480.0, 320.0);
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
self.view=landscapeView;
self.view.transform=CGAffineTransformMakeRotation(deg2rad*(-90));
self.view.bounds=CGRectMake(0.0, 0.0, 480.0, 320.0);
} else {
self.view=portraitView;
self.view.transform=CGAffineTransformMakeRotation(deg2rad*(0));
self.view.bounds=CGRectMake(0.0, 0.0, 300.0, 460.0);
}
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
@end
I have taken out some things that are not necessary, so the .m file may look a little bit bare. I have linked the IBOutlets landscapeView and portraitView to 2 different views within one .xib file called MainViewController.xib
When I build it, there are no errors, warnings, or signals.
Is there an easier way to do this or am I just doing it wrong? Could somebody help?
Thanks in advance!
You need to fix your shouldAutorotateToInterfaceOrientation for one. The return value will always match up with the first, it should look more like