【问题标题】:Unable to understand the orientation in iphone programming?无法理解 iphone 编程中的方向?
【发布时间】:2011-09-15 12:06:04
【问题描述】:

我在 ViewDidLoad 中编写代码如下,

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)                                               name:UIDeviceOrientationDidChangeNotification object:nil]; 
    //Whenever the device orientation changes, orientationChanged will be called

以及orientationChanged的主体:方法如下

    - (void)orientationChanged:(NSNotification *)notification
    {
        UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

        NSLog(@"Method is Called"); 

        self.view.frame = CGRectMake(0, 0, 480, 320);
        //self.view.superview.frame = CGRectMake(-50, -70, 800, 900);

        //Based upon which orientation the device is in, update your View rotations.
        //A simple view.transform = CGAffineTransformMakeRotation(whatever); will work well.
    }

我无法理解这种方法的行为,任何人都可以告诉我这两行会做什么,第一行改变 self.view.frame 的框架,第二行改变我评论的 superview。

在此先感谢 Saeen

【问题讨论】:

  • 你为什么使用 NSNotificationCenter 来做这个
  • 简单地说,我是从堆栈中找到的 :-(
  • 我在下面添加代码尝试使用该代码,您可以从 viewWillAppear 以当前方向调用该方法,例如 [self willAnimateRotationToInterfaceOrientation:[UIApplication sharedApplication].statusBarOrientation duration:0.2];

标签: iphone objective-c orientation


【解决方案1】:

您可以根据方向管理视图。检查我的答案here

【讨论】:

    【解决方案2】:

    这个方法会在你旋转设备时调用,你可以在这个方法中设置框架

         - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
            {
            if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
            {
               self.view.frame = CGRectMake(0, 0, 480, 300);
    
            }
            else
            {
               self.view.frame = CGRectMake(0, 0, 320, 460);
    
            }
         }
    

    【讨论】:

      【解决方案3】:

      使用这个通知中心它工作正常

          -(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      
          - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
          {
              appDelegate.interface=interfaceOrientation;
              [popoverController dismissPopoverAnimated:YES];
              if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
              {  
              if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
              {
                  txt_description.font = [UIFont systemFontOfSize:17];
                  appDelegate.screenWidth = 1024;
                  img_background.frame = CGRectMake(0, 0, 1024, 748);
                  img_background.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_ipad_landscape" ofType:@"png"]];
      
                  lbl_friendName.frame=CGRectMake(185, 58, 180, 30);
                  btn_friendName.frame=CGRectMake(390, 60, 436, 27);
                  [btn_friendName setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
                  lbl_friendNameValue.frame=CGRectMake(399, 59, 300, 30);
                  lbl_date.frame=CGRectMake(185, 110, 180, 30);
                  btn_date.frame=CGRectMake(390, 110, 436, 27);
                  [btn_date setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
                  lbl_dateValue.frame=CGRectMake(399, 109, 300, 30);
                  lbl_category.frame=CGRectMake(185, 160, 180, 30);
                  btn_category.frame=CGRectMake(390, 160, 436, 27);
                  [btn_category setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
                  lbl_categoryValue.frame=CGRectMake(399, 159, 300, 30);
                  //btn_radio2.frame=CGRectMake(199, 128, 21, 21);
                  lbl_amount.frame=CGRectMake(185, 210, 180, 30);
                  txt_amount.frame=CGRectMake(390, 210, 436, 31);
                  lbl_currency.frame=CGRectMake(185, 260, 180, 30);
                  lbl_currencyValue.frame=CGRectMake(399, 258, 330, 30);
                  btn_currency.frame = CGRectMake(390, 260, 436, 27);
                  [btn_currency setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
                  lbl_typeofCost.frame=CGRectMake(185, 310, 180, 30);
                  btn_radio1.frame = CGRectMake(390, 310, 21, 21);
                  lbl_allocate.frame = CGRectMake(420, 308, 180, 27);
                  btn_radio2.frame = CGRectMake(600, 310, 21, 21);
                  lbl_share.frame = CGRectMake(630, 308, 180, 27);
                  lbl_typeofCostValue.frame=CGRectMake(399, 348, 430, 30);
                  btn_typeofCost.frame = CGRectMake(390, 350, 436, 27);
                  [btn_typeofCost setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
                  lbl_Description.frame=CGRectMake(185, 394, 180, 30);
                  img_inputbox.frame=CGRectMake(390, 400, 436, 61);
                  img_inputbox.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"input_box_s14_1_ipad_landscape" ofType:@"png"]];
                  txt_description.frame=CGRectMake(395, 405, 436, 53);
                  lbl_placeholder.frame=CGRectMake(402, 405, 350, 21);
                  btn_save.frame=CGRectMake(430, 550, 128, 51);
                  scrollView.contentSize=CGSizeMake(self.view.frame.size.width, 800);
                  lbl_colon1.frame = CGRectMake(378, 58, 5, 30); 
                  lbl_colon2.frame = CGRectMake(378, 108, 5, 30);
                  lbl_colon3.frame = CGRectMake(378, 158, 5, 30);
                  lbl_colon4.frame = CGRectMake(378, 208, 5, 30);
                  lbl_colon5.frame = CGRectMake(378, 258, 5, 30);
                  lbl_colon6.frame = CGRectMake(378, 304, 5, 30);
                  lbl_colon7.frame = CGRectMake(378, 392, 5, 30);
      
              }
              else
              {
                  appDelegate.screenWidth=480;
                  [scrollView setContentSize:CGSizeMake(480, 440)];
                  img_background.frame=CGRectMake(0, 0, 480,320);
                  img_topbar.frame=CGRectMake(0, 0, 480, 44);
                  img_background.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_landscape" ofType:@"png"]];
                  //lbl_head.frame=CGRectMake(160, 9, 159, 33);
      
                  lbl_friendName.frame=CGRectMake(13, 30, 110, 21);
                  btn_friendName.frame=CGRectMake(130, 27, 286, 27);
                  [btn_friendName setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:0];
      
      
                  lbl_date.frame=CGRectMake(13, 67, 95, 21);
                  btn_date.frame=CGRectMake(130, 64, 286, 27);
                  [btn_date setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:0];
      
                  lbl_category.frame=CGRectMake(13, 106, 95, 21);
                  btn_category.frame=CGRectMake(130, 102, 286, 27);
                  [btn_category setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:0];
                              //btn_radio2.frame=CGRectMake(199, 128, 21, 21);
                  lbl_amount.frame=CGRectMake(13, 143, 95, 21);
                  txt_amount.frame=CGRectMake(130, 140, 286, 31);
                  lbl_currency.frame=CGRectMake(13, 182, 95, 21);
                  btn_currency.frame = CGRectMake(130, 179, 286, 27);
                  [btn_currency setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:0];
      
                  lbl_typeofCost.frame=CGRectMake(13, 212, 95, 21);
                  btn_radio1.frame=CGRectMake(130, 212, 21, 21);
                  lbl_allocate.frame = CGRectMake(155, 212, 100, 21);
                  btn_radio2.frame = CGRectMake(275, 212, 21, 21);
                  lbl_share.frame = CGRectMake(310, 212, 80, 21);
                  btn_typeofCost.frame = CGRectMake(130, 240, 286, 27);
                  lbl_typeofCostValue.frame = CGRectMake(135, 242, 250, 21);
                  [btn_typeofCost setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:UIControlStateNormal];
                  //[btn_provideDate setImage:[UIImage imageNamed:@"dropdown_s5.png"] forState:UIControlStateNormal];
                  lbl_Description.frame=CGRectMake(13, 276, 150, 21);
                  img_inputbox.frame=CGRectMake(130, 275, 286, 61);
                  img_inputbox.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"input_box_s42_landscape" ofType:@"png"]];
                  txt_description.frame=CGRectMake(135, 281, 276, 45);
                  lbl_placeholder.frame=CGRectMake(142, 278, 200, 21);
                  btn_save.frame=CGRectMake(190, 345, 108, 41);
                  //btn_adv.frame=CGRectMake(0, 175, 480, 48);
                  lbl_colon1.frame = CGRectMake(119, 27, 5, 27); 
                  lbl_colon2.frame = CGRectMake(119, 63, 5, 27);
                  lbl_colon3.frame = CGRectMake(119, 102, 5, 27);
                  lbl_colon4.frame = CGRectMake(119, 140, 5, 27);
                  lbl_colon5.frame = CGRectMake(119, 179, 5, 27);
                  lbl_colon6.frame = CGRectMake(119, 208, 5, 27);
                  lbl_colon7.frame = CGRectMake(119, 272, 5, 27);
                  [self displayProperFontSize];
                  }
      
      
              }
              if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationPortrait)
              { 
      
                  if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
                  {
                  txt_description.font = [UIFont systemFontOfSize:17];
                  appDelegate.screenWidth=768;
                  img_background.frame = CGRectMake(0, 0, 768, 1004);
                  img_background.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_ipad" ofType:@"png"]];
      
                  lbl_friendName.frame=CGRectMake(105, 65, 180, 30);
                  btn_friendName.frame=CGRectMake(310, 67, 316, 27);
                  [btn_friendName setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
                  lbl_friendNameValue.frame=CGRectMake(318, 66, 280, 28);
                  lbl_date.frame=CGRectMake(105, 116, 180, 30);
                  btn_date.frame=CGRectMake(310, 116, 316, 27);
                  [btn_date setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
                  lbl_dateValue.frame=CGRectMake(315, 116, 278, 26);
                  lbl_category.frame=CGRectMake(105, 164, 180, 30);
                  btn_category.frame=CGRectMake(310, 166, 316, 27);
                  [btn_category setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
                  lbl_categoryValue.frame=CGRectMake(318, 167, 273, 26);
                  //btn_radio2.frame=CGRectMake(199, 128, 21, 21);
                  lbl_amount.frame=CGRectMake(105, 210, 180, 30);
                  txt_amount.frame=CGRectMake(310, 210, 316, 31);
                  lbl_currency.frame=CGRectMake(105, 260, 180, 30);
                  btn_currency.frame = CGRectMake(310, 258,316, 33);
                  lbl_currencyValue.frame=CGRectMake(316, 260, 310, 30);
                  [btn_currency setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
                  lbl_typeofCost.frame=CGRectMake(105, 310, 180, 30);
                  btn_radio1.frame=CGRectMake(310, 318, 21, 21);
                  lbl_allocate.frame = CGRectMake(340, 318, 100, 21);
                  btn_radio2.frame = CGRectMake(445, 318, 21, 21);
                  lbl_share.frame = CGRectMake(475, 318, 200, 21);
                  btn_typeofCost.frame = CGRectMake(310, 357,316, 33);
                  lbl_typeofCostValue.frame=CGRectMake(315, 359, 310, 30);
                  [btn_typeofCost setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
                  //[btn_provideDate setImage:[UIImage imageNamed:@"dropdown_s5.png"] forState:UIControlStateNormal];
                  lbl_Description.frame=CGRectMake(105, 400, 180, 30);
                  img_inputbox.frame=CGRectMake(310, 410, 316, 61);
                  img_inputbox.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"input_box_s42_1_ipad" ofType:@"png"]];
                  txt_description.frame=CGRectMake(316, 414, 310, 53);
                  lbl_placeholder.frame = CGRectMake(322, 414, 290, 21);
      
                  lbl_colon1.frame = CGRectMake(300, 65, 7, 30); 
                  lbl_colon2.frame = CGRectMake(300, 114, 7, 30);
                  lbl_colon3.frame = CGRectMake(300, 164, 7, 30);
                  lbl_colon4.frame = CGRectMake(300, 209, 7, 30);
                  lbl_colon5.frame = CGRectMake(300, 260, 7, 30);
                  lbl_colon6.frame = CGRectMake(300, 313, 7, 30);
                  lbl_colon7.frame = CGRectMake(300, 402, 7, 30);
      
                  btn_save.frame=CGRectMake(300, 550, 128, 51);
                  } 
                  else
                  {
                     appDelegate.screenWidth=320;
                     [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
                      img_background.frame=CGRectMake(0, 0, 320,480);
                      img_background.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"]];
                      lbl_friendName.frame=CGRectMake(8, 16, 97, 34);
                      btn_friendName.frame=CGRectMake(114, 20, 196, 27);
                      [btn_friendName setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
      
                  lbl_friendNameValue.frame=CGRectMake(121, 20, 170, 28);
                  lbl_date.frame=CGRectMake(8, 59, 95, 21);
                  btn_date.frame=CGRectMake(114, 56, 196, 27);
                  [btn_date setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
                  lbl_dateValue.frame=CGRectMake(121, 53, 170, 34);
                  lbl_category.frame=CGRectMake(8, 96, 95, 21);
                  btn_category.frame=CGRectMake(114, 94, 196, 27);
                  [btn_category setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
                  lbl_categoryValue.frame=CGRectMake(120, 90, 166, 34);
                  //btn_radio2.frame=CGRectMake(199, 128, 21, 21);
                  lbl_amount.frame=CGRectMake(8, 136, 95, 21);
                  txt_amount.frame=CGRectMake(114, 131, 196, 31);
                  lbl_currency.frame=CGRectMake(8, 170, 95, 21);
      
                  btn_currency.frame =CGRectMake(114, 170, 196, 27);
                  [btn_currency setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
                  lbl_typeofCost.frame=CGRectMake(8, 205, 95, 21);
                  btn_radio1.frame=CGRectMake(114, 205, 21, 21);
                  lbl_allocate.frame = CGRectMake(140, 205, 80, 21);
                  btn_radio2.frame = CGRectMake(220, 205, 21, 21);
                  lbl_share.frame = CGRectMake(245, 199, 65, 34);
                  btn_typeofCost.frame = CGRectMake(114, 233, 196, 27);
                  lbl_typeofCostValue.frame = CGRectMake(119, 229, 170, 34);
                  [btn_typeofCost setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
                  //[btn_provideDate setImage:[UIImage imageNamed:@"dropdown_s5.png"] forState:UIControlStateNormal];
                  lbl_Description.frame=CGRectMake(8, 265, 95, 21);
                  img_inputbox.frame=CGRectMake(114, 267, 196, 51);
                  img_inputbox.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"input_box_s16" ofType:@"png"]];
                  txt_description.frame=CGRectMake(117, 274, 185, 37);
                  lbl_placeholder.frame = CGRectMake(124, 270, 160, 21);
                  btn_save.frame=CGRectMake(109, 326, 88, 41);
      
                  lbl_colon1.frame = CGRectMake(105, 20, 5, 27); 
                  lbl_colon2.frame = CGRectMake(105, 55, 5, 27);
                  lbl_colon3.frame = CGRectMake(105, 94, 5, 27);
                  lbl_colon4.frame = CGRectMake(105, 133, 5, 27);
                  lbl_colon5.frame = CGRectMake(105, 168, 5, 27);
                  lbl_colon6.frame = CGRectMake(105, 201, 5, 27);
                  lbl_colon7.frame = CGRectMake(105, 262, 5, 27);
                  [self displayProperFontSize];
                  }   
              }
              [appDelegate adjustTab];
      
      
          }
      

      【讨论】:

      • 让我搜索这个词,如果您提供几行代码,我将不胜感激,我想根据方向和动画更改标签 CGRectMake。
      • 哇..看起来很简单,不费吹灰之力:D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多