Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @implementation PortraitViewController
- -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
- if (self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil]) {
- self.isShowingLandscapeView = NO;
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(orientationChanged:)
- name:UIDeviceOrientationDidChangeNotification
- object:nil];
- }
- return self;
- }
- - (void)viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
- [self setupByOrientation:[self interfaceOrientation]];
- }
- - (void)orientationChanged:(NSNotification *)notification{
- UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
- if (!UIDeviceOrientationIsValidInterfaceOrientation(orientation)) {
- orientation = [self interfaceOrientation];
- }
- [self setupByOrientation:orientation];
- }
- - (void)setupByOrientation:(UIDeviceOrientation) orientation{
- if (UIDeviceOrientationIsLandscape(orientation) && !self.isShowingLandscapeView){
- [UIView beginAnimations:@"" context:nil];
- [UIView setAnimationDuration:.4];
- [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:YES];
- if ([self.textView isFirstResponder]) {
- [self.textView resignFirstResponder];
- }
- NSLog(@"Going Landscape");
- if (self.landscapeViewController != nil) {
- [self.landscapeViewController release];
- }
- self.landscapeViewController = [[LandscapeViewController alloc] initWithNibName:@"LandscapeViewController" bundle:nil];
- self.landscapeViewController.entry = self.entry;
- [self presentModalViewController:self.landscapeViewController animated:NO];
- self.isShowingLandscapeView = YES;
- [UIView commitAnimations];
- }
- else if (UIDeviceOrientationIsPortrait(orientation) && self.isShowingLandscapeView){
- self.isShowingLandscapeView = NO;
- [UIView beginAnimations:@"" context:nil];
- [UIView setAnimationDuration:.4];
- NSLog(@"Going Portrait");
- [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:NO];
- NSLog(@"Our Entry: %@", self.entry);
- [self dismissModalViewControllerAnimated:NO];
- [self setupView];
- [UIView commitAnimations];
- }
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- if (UIDeviceOrientationIsPortrait(interfaceOrientation)) {
- return YES;
- } else if (UIDeviceOrientationIsLandscape(interfaceOrientation)) {
- return YES;
- }
- return NO;
- }
- @end
Add Comment
Please, Sign In to add comment