Advertisement
Guest User

Untitled

a guest
May 29th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. // NSNotificationCenter の通知を利用
  2.  
  3. // UIKeyboardDidChangeFrameNotification
  4. // キーボードのサイズが変更する時
  5. // キーボードが出るたびに毎回呼ばれる
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewWillAppear:(BOOL)animated
  10. {
  11. [super viewWillAppear:animated];
  12.  
  13. // キーボードのサイズが変化するとき
  14. [[NSNotificationCenter defaultCenter] addObserver:self
  15. selector:@selector(keyboarDidChangeFrame:)
  16. name:UIKeyboardDidChangeFrameNotification
  17. object:nil];
  18. }
  19.  
  20. - (void)viewWillDisappear:(BOOL)animated
  21. {
  22. [super viewWillDisappear:animated];
  23.  
  24. [[NSNotificationCenter defaultCenter] removeObserver:self
  25. name:UIKeyboardDidChangeFrameNotification
  26. object:nil];
  27. }
  28.  
  29. // キーボードのサイズが変化するとき
  30. - (void)keyboarDidChangeFrame:(NSNotificationCenter*)notification
  31. {
  32. UIInterfaceOrientation orientation = self.interfaceOrientation;
  33. NSString *interfaceOrientation = [self getOrientation:orientation];
  34. NSLogE(@"現在の画面の向き: %@", interfaceOrientation);
  35. }
  36.  
  37. - (NSString*)getOrientation:(UIInterfaceOrientation)interfaceOrientation
  38. {
  39. NSString *str;
  40.  
  41. if(interfaceOrientation == UIInterfaceOrientationPortrait){
  42. str = @"portrait";
  43. } else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
  44. str = @"landscape left";
  45. } else if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
  46. str = @"upsidedown";
  47. } else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight){
  48. str = @"landscape right";
  49. }
  50.  
  51. NSLogE(@"現在の画面の向き: %@", str);
  52.  
  53. return str;
  54. }
  55.  
  56. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement