Guest User

Untitled

a guest
Jun 24th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. @implementation PortraitViewController
  2.  
  3. -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
  4. if (self = [super initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil]) {
  5. self.isShowingLandscapeView = NO;
  6. [[NSNotificationCenter defaultCenter] addObserver:self
  7. selector:@selector(orientationChanged:)
  8. name:UIDeviceOrientationDidChangeNotification
  9. object:nil];
  10. }
  11. return self;
  12. }
  13.  
  14. - (void)viewDidAppear:(BOOL)animated{
  15. [super viewDidAppear:animated];
  16. [self setupByOrientation:[self interfaceOrientation]];
  17. }
  18.  
  19. - (void)orientationChanged:(NSNotification *)notification{
  20. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  21. if (!UIDeviceOrientationIsValidInterfaceOrientation(orientation)) {
  22. orientation = [self interfaceOrientation];
  23. }
  24.  
  25. [self setupByOrientation:orientation];
  26. }
  27.  
  28. - (void)setupByOrientation:(UIDeviceOrientation) orientation{
  29. if (UIDeviceOrientationIsLandscape(orientation) && !self.isShowingLandscapeView){
  30. [UIView beginAnimations:@"" context:nil];
  31. [UIView setAnimationDuration:.4];
  32.  
  33. [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:YES];
  34. if ([self.textView isFirstResponder]) {
  35. [self.textView resignFirstResponder];
  36. }
  37. NSLog(@"Going Landscape");
  38. if (self.landscapeViewController != nil) {
  39. [self.landscapeViewController release];
  40. }
  41. self.landscapeViewController = [[LandscapeViewController alloc] initWithNibName:@"LandscapeViewController" bundle:nil];
  42. self.landscapeViewController.entry = self.entry;
  43.  
  44. [self presentModalViewController:self.landscapeViewController animated:NO];
  45. self.isShowingLandscapeView = YES;
  46. [UIView commitAnimations];
  47. }
  48. else if (UIDeviceOrientationIsPortrait(orientation) && self.isShowingLandscapeView){
  49. self.isShowingLandscapeView = NO;
  50.  
  51. [UIView beginAnimations:@"" context:nil];
  52. [UIView setAnimationDuration:.4];
  53.  
  54. NSLog(@"Going Portrait");
  55. [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:NO];
  56.  
  57. NSLog(@"Our Entry: %@", self.entry);
  58.  
  59. [self dismissModalViewControllerAnimated:NO];
  60. [self setupView];
  61. [UIView commitAnimations];
  62. }
  63. }
  64.  
  65. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  66. if (UIDeviceOrientationIsPortrait(interfaceOrientation)) {
  67. return YES;
  68. } else if (UIDeviceOrientationIsLandscape(interfaceOrientation)) {
  69. return YES;
  70. }
  71. return NO;
  72. }
  73.  
  74. @end
Add Comment
Please, Sign In to add comment